Ad
Log in to vote
1

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

Asked by 5 days ago
Edited 5 days 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 5 days 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 41 — 5d
0
No problem Sabailuridze 126 — 5d
Ad

Answer this question