Below is a script that lowers a rock into lava, so you die if you stand on it too long, but if you stand on it without jumping off of it, it reacts weird because it sort of resets the script when you touch it again. I am looking for a way to make the touched function not to do anything with the input while the script is running. Thanks in advance for any help!
function touch(hit) if hit.Parent ~= nil then local player = game.Players:playerFromCharacter(hit.Parent) if player ~= nil then script.Parent.Parent.first.Transparency = 1 script.Parent.Parent.first.CanCollide = false script.Parent.Parent.second.Transparency = 0 wait(0.1) script.Parent.Parent.second.Transparency = 1 script.Parent.Parent.second.CanCollide = false script.Parent.Parent.third.Transparency = 0 wait(0.1) script.Parent.Parent.third.Transparency = 1 script.Parent.Parent.third.CanCollide = false script.Parent.Parent.fourth.Transparency = 0 wait(0.1) script.Parent.Parent.fourth.Transparency = 1 script.Parent.Parent.fourth.CanCollide = false script.Parent.Parent.fifth.Transparency = 0 wait(0.1) script.Parent.Parent.fifth.Transparency = 1 script.Parent.Parent.fifth.CanCollide = false script.Parent.Parent.sixth.Transparency = 0 wait(0.1) script.Parent.Parent.sixth.Transparency = 1 script.Parent.Parent.sixth.CanCollide = false wait(1) --RESET ROCK-- script.Parent.Parent.first.Transparency = 0 script.Parent.Parent.first.CanCollide = true script.Parent.Parent.second.CanCollide = true script.Parent.Parent.third.CanCollide = true script.Parent.Parent.fourth.CanCollide = true script.Parent.Parent.fifth.CanCollide = true script.Parent.Parent.sixth.CanCollide = true end end end script.Parent.Touched:connect(touch)
You'll have to use something called debounce
. Debounce
is basically using a system to ensure that only 1 input is taken while a program is running. To do this, you would have to use a variable, and check whether the variable is a certain value before running the code, like so:
local Active = true function Test() if Active then --Checks to see if the variable "Active" is true Active = false --Sets the variable "Active" to false so the code won't run multiple times at once --Code Active = true --Sets the variable "Active" to true so the code can run again end end
In your case, you would put the code that you have in your function where it says Code, like so:
local Active = true function touch(hit) if Active then Active = false if hit.Parent ~= nil then local player = game.Players:playerFromCharacter(hit.Parent) if player ~= nil then script.Parent.Parent.first.Transparency = 1 script.Parent.Parent.first.CanCollide = false script.Parent.Parent.second.Transparency = 0 wait(0.1) script.Parent.Parent.second.Transparency = 1 script.Parent.Parent.second.CanCollide = false script.Parent.Parent.third.Transparency = 0 wait(0.1) script.Parent.Parent.third.Transparency = 1 script.Parent.Parent.third.CanCollide = false script.Parent.Parent.fourth.Transparency = 0 wait(0.1) script.Parent.Parent.fourth.Transparency = 1 script.Parent.Parent.fourth.CanCollide = false script.Parent.Parent.fifth.Transparency = 0 wait(0.1) script.Parent.Parent.fifth.Transparency = 1 script.Parent.Parent.fifth.CanCollide = false script.Parent.Parent.sixth.Transparency = 0 wait(0.1) script.Parent.Parent.sixth.Transparency = 1 script.Parent.Parent.sixth.CanCollide = false wait(1) --RESET ROCK-- script.Parent.Parent.first.Transparency = 0 script.Parent.Parent.first.CanCollide = true script.Parent.Parent.second.CanCollide = true script.Parent.Parent.third.CanCollide = true script.Parent.Parent.fourth.CanCollide = true script.Parent.Parent.fifth.CanCollide = true script.Parent.Parent.sixth.CanCollide = true end end Active = true end end script.Parent.Touched:connect(touch)
Hope this helped!
just use a flag variable
example
local flag = false function touch(hit) if flag == false then flag = true --run code here flag = false end end