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

Why is debounce and waits not working in my script?

The title is kinda click bait because other titles weren't working for some reason. About an hour ago I asked why my script wasn't working and so someone helped me out, but when I add a wait so I doesn't give me infinite amounts of cash. But it doesn't work. Any help?

local leaderstats -- We need to get the player before we do anything. Leave blank for now.
local playersService = game:GetService"Players"
local debounce = false -- if you don't want it to spam "Raw Robloxs" add this.

script.Parent.Touched:Connect(function(part)
    if not debounce and part.Parent.Name == "Pick" then
        local plr = playersService:GetPlayerFromCharacter(part.Parent.Parent)
        if plr then -- make sure it's a player
            debounce = true
            leaderstats = plr:WaitForChild"leaderstats"
            leaderstats["Raw Robloxs"].Value = leaderstats["Raw Robloxs"].Value + 5
            -- debounce = false -- Debounce reset logic can go here. Reset to false so the code can repeat. If you wish for it to not repeat, take this off and it can be a one time touch.
			wait(1)
       end
    end
end)

script.Parent.TouchEnded:Connect(function(part)
    debounce = false -- Debounce logic can go here if you want it to reset after it stops touching.
end)

2 comments

0

Thx for using my script xdd.I'll fix it

0

Thanks!

1 answer

✓ Accepted answer
local leaderstats -- We need to get the player before we do anything. Leave blank for now.
local playersService = game:GetService"Players"
local debounce = false -- if you don't want it to spam "Raw Robloxs" add this.
local moneyToGive = 5
 
script.Parent.Touched:Connect(function(part)
    if not debounce and part.Parent.Name == "Pick" then
        local plr = playersService:GetPlayerFromCharacter(part.Parent.Parent)
        if plr then -- make sure it's a player
            debounce = true
            leaderstats = plr:WaitForChild"leaderstats"
            leaderstats["Raw Robloxs"].Value = leaderstats["Raw Robloxs"].Value + moneyToGive
            -- debounce = false -- Debounce reset logic can go here. Reset to false so the code can 
repeat. If you wish for it to not repeat, take this off and it can be a one time touch.
		moneyToGive = 0 -- set to 0 so it will just give 0 cash. 
            wait(1)
       end
    end
end)

script.Parent.TouchEnded:Connect(function(part)
    debounce = false -- Debounce logic can go here if you want it to reset after it stops touching.
end)

4 comments

0

When I use the script it doesn't reset. I can only use it once... Can you make it reusable?

0

Also, you have been a great help. Teaching me about this stuff.

0

Just put debounce = false after the wait(1)

0

I just made a simple regen script. Thanks for the help anyways.

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