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

How do i add a debug to a touch part?

So i made a simple script to give players cash when they touch a part, I added a debug so the players don't get a bunch of cash at once but my debug inst working, I've always had trouble with debugs and understanding them so any help will be great.

Debug = false
script.Parent.Touched:Connect(function(hit)
	
	local player = hit.Parent:FindFirstChild("Humanoid")
	if player and Debug == false then 
		Debug = true
		local Char = game.Players:GetPlayerFromCharacter(hit.Parent)
		Char.leaderstats.Cash.Value = Char.leaderstats.Cash.Value + 5
		Debug = false
	end

end)

1 answer

✓ Accepted answer

Ok, so the problem with your script is that there is no Wait() in there, Wait() is basically waiting for the inserted number of seconds, after that, the script will continue. In your case, you just need to add a Wait() and it will be all good.

(I think most people call it "Debounce" instead of "Debug" this is because "Debug" is a whole diffrent thing.)

More info on Wait(): https://developer.roblox.com/en-us/articles/keeping-time

Modified Script:

local Debug = false

script.Parent.Touched:Connect(function(hit)
    local player = hit.Parent:FindFirstChild("Humanoid")
    if player and Debug == false then 
        Debug = true
        local Char = game.Players:GetPlayerFromCharacter(hit.Parent)
        Char.leaderstats.Cash.Value = Char.leaderstats.Cash.Value + 5
        wait(1) -- you can insert the amount of seconds you want to wait within the brackets.
        Debug = false
    end
end)

Hope this helps :)

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