Roblox script running multiple times? - lua

I need a bit of help. Basically I have this code:
local plyIsEntered = false
function onTouched(hit)
plyIsEntered = true
if not plyIsEntered then
end
if plyIsEntered then
local humanoid = hit.Parent:FindFirstChild("Humanoid")
local ply = humanoid.Parent
if humanoid ~= nil then
print("Hit")
local playerName = hit.Parent.Name
print(playerName)
local laserEmitter = game.Workspace["Enterance PC"]:FindFirstChild("laserEmitter")
local scanLaser = Instance.new("Part", game.Workspace)
scanLaser.Position = laserEmitter.Position
scanLaser.Name = "ScanLaser"
scanLaser.Size = Vector3.new(1,1,1)
local scanLaserMesh = Instance.new("SpecialMesh", game.Workspace.ScanLaser)
scanLaserMesh.Name = "Cone mesh"
scanLaserMesh.MeshType = ""
plyIsEntered = false
end
end
end
script.Parent.Touched:connect(onTouched)
Now I'm checking if the player touches a box, it has no collisions and is invisible; when they do I want to create a laser that will scan them and open a door. The problem I'm having is when I walk into the trigger box it creates 8 or 9 blocks. One of those blocks is the block I'm applying a mesh too.
What I need to do is make sure it's only running once and not creating more than 1 brick. Hopefully someone can help me!

I believe to fix this you'll need to add a debounce.
You see, the touched event of a Part actually fires many times, so your code will execute multiple times if it is inside of the event.
To fix this, we use a debounce, which means your code won't execute if your part is touched too much in the same time frame. Here is an example:
local debounce = false
part.Touched:connect(function()
if debounce == false then
debounce = true
--Your code goes here.
wait(1)--Wait one second until you'll be able to execute the code again.
debounce = false
end
end)
To read more on debounces: http://wiki.roblox.com/index.php?title=Debounce

Related

Tool ability only being able to be used once?

So, I have a tool that allows the player to dash, yet for some reason the cooldown doesnt seem to work correctly. Does anyone know why the player can't use the ability again?
-- Locals --
local Tool = script.Parent
local Cooldown = false
local mouse=game.Players.LocalPlayer:GetMouse()
local sound=Instance.new("Sound")
-- Main Script --
mouse.KeyDown:Connect(function(key)
if key=="e" then
if Cooldown == false then
Cooldown = true
sound.Parent=Tool.Handle
sound.Volume=3
sound.SoundId="rbxassetid://9079020013"
sound:Play()
game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 60
wait(.5)
sound:Destroy()
game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 16
wait(5)
Cooldown = false
end
end
end)
The goal is for the player to have an E ability that makes them dash, play a sound effect, then stop after half a second. Then, 5 seconds later, the player could do it again. All of this happened, except the player wasnt able to use the ability again.
Have you try this yet, a tool can be activated.
script.Parent.activated:Connect(function()
local sound=Instance.new("Sound")
if Cooldown == false then
Cooldown = true
sound.Parent=Tool.Handle
sound.Volume=3
sound.SoundId="rbxassetid://9079020013"
sound:Play()
game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 60
wait()
sound:Destroy()
game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 16
wait(5)
Cooldown = false
end
end)

Script isn't detecting a change with a BoolValue (Roblox Studio)

I'm trying to make a procedurally generating game (ignore the goal of the game).
Currently I'm trying to make it so when you walk over a grid piece and collide with an invisible part it makes that specific grid piece the Current Global Object (CGO).
Using a BoolValue I made it possible to use this as some form of global variable, but whenever I try to use said value it only detects it as false even when it is indeed showing as true in the Explorer. It only works when I set the value to true BEFORE testing the game.
Here's the code in the script that's meant to detect the value:
it's a regular script and not a LocalScript fyi
local CGO = "0,0"
local tracker = game.Workspace.Tracker00
--local CGOa = tracker.CGOa
local trackerPos = tracker.Position
local trackerX = trackerPos.X
local trackerY = trackerPos.Y
local trackerZ = trackerPos.Z
while true do
while tracker.CGOa.Value == false do
tracker.YVal.Value = 7
print("Set TrackerYVal to 7")
wait()
if tracker.CGOa.Value == true then
break
end
end
while tracker.CGOa.Value == true do
tracker.YVal.Value = 14
print("Set TrackerYVal to 14")
wait()
end
tracker.CFrame = CFrame.new(trackerX, trackerY, trackerZ)
wait()
end
Any help would be much appreciated.
Rather than using infinite while loops, consider listening for the Changed signal. It's possible that the break command might be escaping both loops.
local tracker = game.Workspace.Tracker00
tracker.CGOa.Changed:Connect(function(newVal)
print("CGOa changed to ", newVal)
if newVal then
tracker.YVal.Value = 14
print("Set TrackerYVal to 14")
else
tracker.YVal.Value = 7
print("Set TrackerYVal to 7")
end
-- update the tracker position based on newly updated values
local x = tracker.XVal.Value
local y = tracker.YVal.Value
local x = tracker.ZVal.Value
tracker.CFrame = CFrame.new(Vector3.new(x, y, z))
end)
I made some assumptions about how you were positioning the tracker, because the old code would reset its position after every loop.

Punch animation from Roblox tutorial is throwing errors

I'm trying to get a punch animation to work from a tutorial, but it never works when I press the designated Keybinding.
I have tried changing values from the tutorial from 1 to the animation length, past the animation length, and even random numbers. I also tried changing some wording to the script.
math.randomseed(tick())
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local punchEvent = Instance.new("RemoteEvent", ReplicatedStorage)
punchEvent.Name = "PunchEvent"
local animation = (03910055905)
local function onPunchFired(plr)
local char = game.Workspace:FindFirstChild(plr.Name)
local humanoid = char.Humanoid
local animation = Instance.new("Animation")
animation.AnimationId = "http://roblox.com/asset/?id="..animations[math.random(1, #animations)]
local animTrack = humanoid:LoadAnimation(animation)
animTrack:Play()
end
punchEvent.OnServerEvent:Connect(onPunchFired)
I expect the script to run smooth and punch, but it shows this:
20:46:29.496 - ServerScriptService.ExtremePunch:13: attempt to get length of global 'animations' (a nil value)
This is all it says in the output for errors. I've double checked the replication, but it keeps failing. How can I fix it?
Your error is pointing at this line :
animation.AnimationId = "http://roblox.com/asset/?id="..animations[math.random(1, #animations)]
This is because in the scope of this script, there is no local variable or array named animations.
To fix your code, you just have to fix the number in the animation.AnimationId url. One problem you might have run into was that you had two variables named animation so it failed to construct the url properly for you.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local punchEvent = Instance.new("RemoteEvent", ReplicatedStorage)
punchEvent.Name = "PunchEvent"
local function onPunchFired(plr)
local char = game.Workspace:FindFirstChild(plr.Name)
local humanoid = char.Humanoid
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://3910055905"
local animTrack = humanoid:LoadAnimation(animation)
animTrack:Play()
end
punchEvent.OnServerEvent:Connect(onPunchFired)
If you're unsure if your asset id is correct, you can also check by putting it into the url for the Roblox Catalog : https://www.roblox.com/catalog/YOUR_ASSET_ID_NUMBER. If it redirects to your animation, you know you've got the right one.
The animations list does not exist in the code that you showed us. So, add your other punch animations to the animations list that I added and you should be good to go. I didn't test it so if it's not working, HMU.
local replicatedStorage = game:GetService("ReplicatedStorage")
local punchEvent = Instance.new("RemoteEvent")
punchEvent.Name = "PunchEvent"
punchEvent.Parent = replicatedStorage
local animations = [03910055905, ...]
local function onPunchFired(plr)
local char = game.Workspace:FindFirstChild(plr.Name)
local humanoid = char.Humanoid
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://" .. tostring(animations[math.random(1, #animations)])
local animTrack = humanoid:LoadAnimation(animation)
animTrack:Play()
end
punchEvent.OnServerEvent:Connect(onPunchFired)

My script in Roblox works fine, but once I added a debounce, it still worked perfectly, but only sometimes?

For example: The script works fine in one game session, but then in another, it doesn't work at all; almost as if there's some sort of random chance for the script to be deleted or completely ignored. If I remove the debounce, there's a 100% chance for the script to work once again. What could possibly be going wrong here?
local radius = script.Parent
local light = radius.Parent.Light
local sound = radius.Parent.lighton
local debounce = false
radius.Touched:connect(function(hit)
if debounce == false then debounce = true
if game.Players:GetPlayerFromCharacter(hit.Parent) then
light.PointLight.Brightness = 10
light.Material = "Neon"
sound:Play()
wait(5.5)
light.PointLight.Brightness = 0
light.Material = "Plastic"
sound:Play()
wait(0.5)
debounce = false
end
end
end)
Your problem is one of scoping. The debounce will always be set to true, but will only sometimes be set back to false. If it doesn't get changed, the function will obviously never be run again. You'll want to avoid lines like if debounce == false then debounce = true, as they make it more difficult for you to notice that the debounce is not changed in the same scope.
Fixed code:
local radius = script.Parent
local light = radius.Parent.Light
local sound = radius.Parent.lighton
local debounce = false
radius.Touched:connect(function(hit)
if debounce == false then
debounce = true
if game.Players:GetPlayerFromCharacter(hit.Parent) then
light.PointLight.Brightness = 10
light.Material = "Neon"
sound:Play()
wait(5.5)
light.PointLight.Brightness = 0
light.Material = "Plastic"
sound:Play()
wait(0.5)
end
debounce = false
end
end)
Note that both statements changing the value of debounce align.

ROBLOX Get name of a player that clicked a brick

I have this script in a brick:
local giver = 1
function onClicked()
game.Players.[I NEED THE PLAYER NAME HERE].leaderstats.Clicks.Value = game.Players.[I NEED THE PLAYER NAME HERE].leaderstats.Clicks.Value + giver
end
script.Parent.ClickDetector.MouseClick:connect(onClicked)
Now I need to somehow get the player's name that clicked it and put it where I need to.
The ClickDetectors's MouseClick event have the "Clicking Player" as parameter, so you can do it like this:
local giver = 1
function onClicked(Player)
Player.leaderstats.Clicks.Value = Player.leaderstats.Clicks.Value + giver
end
script.Parent.ClickDetector.MouseClick:connect(onClicked)
However, this requires the FilteringEnabled to be set to false (not recomended).
To solve this, make a LocalScript in the brick with the code:
script.Parent.ClickDetector.MouseClick:connect(function(Player)
game.ReplicatedStorage:WaitForChild("BrickClick"):InvokeServer(script.Parent)
end)
And in a Script placed in the ServerScriptService put:
local Listener = game.ReplicatedStorage:FindFirstChild("BrickClick")
if Listener == nil then
Listener = Instance.new("RemoteFunction")
Listener.Name = "BrickClick"
Listener.Parent = game.ReplicatedStorage
end
function Listener.OnServerInvoke(Player,Brick)
Player.leaderstats.Clicks.Value = Player.leaderstats.Clicks.Value + 1
end
I won't point you to the wiki page for further reading, even thought it contains a bit of what you need, it contains too little information.
The ClickDetector's MouseClick info, the guide about FilteringEnabled and the guide about RemoteFunctions are better.
Try this!
script.Parent.MouseClick:Connect(function(Player)
-- Kill The Player
-- The parameter is referring to game.Players So if you want to do a kill button use .Character
Player.Character:BreakJoints()
-- Change The Color To Red (Other details)
script.Parent.Parent.BrickColor = BrickColor.new("Really red")
script.Parent.MaxActivationDistance = 0
-- Wait 4 Secs
wait(5)
-- Change The Color To Green
script.Parent.Parent.BrickColor = BrickColor.new("Lime green")
script.Parent.MaxActivationDistance = 50
end)

Resources