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

Can I add a delay to the touched event?

I want to add a delay to the touched event, so when it fires it only does one thing, cause at the moment I used it to make this

script.Parent.Touched:Connect(function(plr)
	script.Parent.level.Value = script.Parent.level.Value + 1
	if script.Parent.level.Value == 1 then
	script.Parent.Parent.Transparency = 0.25
	script.Parent.Parent.BrickColor = BrickColor.new(213, 115, 61)
	elseif script.Parent.level.Value == 2 then
	script.Parent.Parent.Transparency = 0.5
	end
	
end)

but it fires like 27 times and results in my script being bassicaly useless.

1 answer

✓ Accepted answer

Hello, Fako16!

You need to add debounce

(Script Editted)

local deb = true

script.Parent.Touched:Connect(function(plr)
    if deb then
	    deb = false
	    script.Parent.level.Value = script.Parent.level.Value + 1
	    if script.Parent.level.Value == 1 then
		    script.Parent.Parent.Transparency = 0.25
		    script.Parent.Parent.BrickColor = BrickColor.new(213, 115, 61)
	    elseif script.Parent.level.Value == 2 then
	    	script.Parent.Parent.Transparency = 0.5
		end -- You forgot this end...
	    wait(5) -- Time between activations....
		deb = true
    end
end)

Good Luck with your games

7 comments

0

Thanks for your response, but it still doesn't work, it just waits 2 seconds and the numbervalue just goes to 27 again

0

Nevermind, you edited your message. I'll test it out now.

0

I did what you recommended, but it still does the same thing, it first goes to 2, then 12 and then 27, and so on, it doesn't fix my question, I tried making the part that activates the script smaller, but it didn't work, I set walkspeed to 33, but it still does not work and does the same thing. Any suggestions?

0

I'll test this, gimme a minute

0

Why should it wait if the condition is false?

0

Solved(And tested)

0

Works. Thanks for your answer!

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