Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

debouncing not working correctly after first execute?

So I made this moving door script: when you touch a button, the door moves waits then moves back and I tried using the wiki to debounce it now after the firstn activation the button works normally but then onwards the button becomes possible to activate earlier than supposed making the door move farther:

local buttonPressed = false

 
script.Parent.Touched:connect(function(move)
    if not buttonPressed then
 
        buttonPressed = true
script.Parent.BrickColor = BrickColor.new(21)
        

local wall = script.Parent.Parent.wall.wall
local dis =150

	for i=0, dis do
		wall.CFrame = wall.CFrame + Vector3.new(0,0,0.2)
		wait()
	end
	
	wait(5)
	for i=0, dis do
	wall.CFrame = wall.CFrame + Vector3.new(0,0,-0.2)
	wait()	
	end
end
 
        wait(20)
script.Parent.BrickColor = BrickColor.new(309)
        buttonPressed = false
       
    end)

2 answers

Alright so it's just a very small mistake that was causing that, you did not have the wait and the debounce change under the if statement, which caused that problem. I've also added a few things to make sure it does not glitch in any way. So here is the script:

local buttonPressed = false

script.Parent.Touched:connect(function(part) 
	if not buttonPressed and part.Parent:findFirstChild("Humanoid", true) then -- Checks if the 'toucher' is a player
		buttonPressed = true
		script.Parent.BrickColor = BrickColor.new(21)
		local wall = script.Parent.Parent.wall.wall
		local dis =150
		for i=0, dis do
    		wall.CFrame = wall.CFrame + Vector3.new(0,0,0.2)
    		wait()
		end 
		wait(5)
		for i=0, dis do
			wall.CFrame = wall.CFrame + Vector3.new(0,0,-0.2)
			wait() 
		end
		wait(5) -- reduced it, thought 20 seconds was too much
		script.Parent.BrickColor = BrickColor.new(309) 
		buttonPressed = false
	end -- this end was right after the end on top of the wait(5) which caused the problem
end)

That had to be under the if statement because when it was not under the if statement it would do it anyways when the button is touched, even if it doesn't meet the conditions, which is what caused the problem.

Hope that helped, if you have any questions, I will be glad to answer you.

1 comment

0

Ooooh I see it now, thanks! I just began learning scripting and my scripts are kind of messy. Thanks for the help! :)

Have you tried changing

 if not buttonPressed then

to

if buttonPressed == false then

It's just a assumption - mainly because you are changing buttonPressed back to false at the end of the script.

This discussion is preserved as it was. New answers, comments, and votes are closed.