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

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

Asked by
Gigaset39 111
2 years ago
Edited 2 years ago

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

Log in to vote
2
Answered by 2 years ago

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

1
It does exactly what i wanted, thankyou. Gigaset39 111 — 2y
0
No problem Sabailuridze 126 — 2y
Ad

Answer this question