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

Script should launch just once,but it does that infinitely. why?

I got a part, that if you touch it, you get points:

 script.Parent.Touched:Connect(function(hit)
if hit and hit.Parent:FindFirstChild("Humanoid") then
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	wait(1) 
	player.leaderstats.Money.Value += 10
	player.leaderstats.Level.Value += 1
	
end

end)

But if you keep touching the part you still get money, but i want it to happen just once,what i should add?

1 answer

✓ Accepted answer

You can add debounce,

local waittime = 2
local isTouched = false 

 script.Parent.Touched:Connect(function(hit)
if hit and hit.Parent:FindFirstChild("Humanoid") and not isTouched then
    isTouched = true
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    wait(1) 
    player.leaderstats.Money.Value += 10
    player.leaderstats.Level.Value += 1
    wait(waittime)
    isTouched = false

end
end)

Have not tested the code, tell me if there are any errors

2 comments

1

It does exactly what i wanted, thankyou.

0

No problem

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