Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

On Touch block issue. Spams item when touched?

Asked by 4 years ago

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

Log in to vote
2
Answered by 4 years ago
Edited 4 years ago

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

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 ImprinintInc 18 — 4y
0
wait i just realised the script doesnt seem to be working.. :/ ImprinintInc 18 — 4y
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 jediplocoon 877 — 4y
0
If it still doesn't work just tell me, and I'll do my best to fix it jediplocoon 877 — 4y
Ad

Answer this question