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

On Touch block issue. Spams item when touched?

I have a touch script where if you touch the part, it takes a model from repstore and parents it to workspace, sort of like a regen button. problem is that, when you touch it, it keeps on spawning the item from repstore constantly until you step off the block. help.


parent = script.Parent

function onTouch(hit)

wait(1)

local ItemInStorage = game.ReplicatedStorage.Floaty;
local P = ItemInStorage:Clone();
P.Parent = workspace;
wait(10)
game.Workspace.Floaty:Destroy()
end



parent.Touched:connect(onTouch)

1 answer

✓ Accepted answer

You might want to add a debounce, or a cooldown in other words, so It might look like this

local debounce = false
parent = script.Parent

function onTouch(hit)
	if debounce == false then
		debounce = true
		wait(1)
		local ItemInStorage = game.ReplicatedStorage.Floaty;
		local P = ItemInStorage:Clone();
		P.Parent = workspace;
		wait(10)
		game.Workspace.Floaty:Destroy()
	end
	wait(10)--change this number to how long you want the cooldown to be
	debounce = false
end

parent.Touched:connect(onTouch)

Forgive me if I'm wrong, I'm pretty new to coding myself, but this should work

4 comments

0

k thanks, im pretty sure i heard of a debounce before, but i thought it was for other things and didnt know how to do it. but thanks

0

wait i just realised the script doesnt seem to be working.. :/

0

And i just noticed an error with the debounce so I moved it up to the beginning of the if statement and it works now, if you use the edited code it should work

0

If it still doesn't work just tell me, and I'll do my best to fix it

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