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

Adds multiple points to the leaderboard?

Asked by 9 years ago

Ive got a script so when you touch it, it adds 1 win to the leaderboard, but when i hit it, it adds multiple ones. How would i only make it add 1?

wins = 1
function onTouched(part)
    local humanoid = part.Parent:findFirstChild("Humanoid")
    if (humanoid~=nil) then
        local thisplr = game.Players:findFirstChild(humanoid.Parent.Name)
        if (thisplr~=nil) then
            local stats = thisplr:findFirstChild("leaderstats")
            if (stats~=nil) then
                local score = stats:findFirstChild("Wins")
                if (score~=nil) then
                    score.Value = score.Value + wins
                    wait(0.1)
                    script.Parent:Destroy()


                end
            end
        end
    end
end
script.Parent.Touched:connect(onTouched)

1 answer

Log in to vote
1
Answered by
Im_Kritz 334 Moderation Voter
9 years ago

The problem is that your button does not have a debounce. When a player hits on your button, they are actually touching it multiple times.

Here is your code with cooldown:

wins = 1
local Debounce = false
function onTouched(part)
    local humanoid = part.Parent:findFirstChild("Humanoid")
    if (humanoid~=nil) then
    if Debounce then return end
    Debounce = not Debounce
        local thisplr = game.Players:findFirstChild(humanoid.Parent.Name)
        if (thisplr~=nil) then
            local stats = thisplr:findFirstChild("leaderstats")
            if (stats~=nil) then
                local score = stats:findFirstChild("Wins")
                if (score~=nil) then
                    score.Value = score.Value + wins
                    wait(0.1)
                    script.Parent:Destroy()


                end
            end
        end
    end
end
script.Parent.Touched:connect(onTouched)

Ad

Answer this question