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

Why does it change all the players leaderstats?

Asked by
Rynappel 212 Moderation Voter
5 years ago

So when I join my game, and touch a block it changes my leaderstats called "Level". But when I test it out with multiple people, and I touch the block. It changes all the people leaderstats to given value. How can I fix this?

game.Players.PlayerAdded:Connect(function(player)
    local debounce = true
    script.Parent.Touched:Connect(function()
        local plrstats = player.leaderstats.Level
        if debounce then
            debounce = false
            plrstats.Value = 1
            wait (1)
            debounce = true
        end
    end)
end)
0
You are creating a .Touched event everytime a player joins, and plus you didn't specify which player touch the brick, so 1 player touch the brick, everyone's leaderstats will be changed Konethorix 197 — 5y
0
so how would I fix this? Rynappel 212 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Because the parameter for game.Players.PlayerAdded:Connect(function() is player, at which the player parameter is connected to all the players who joined the game. You should base your script on the touched function, which returns individual parts that touched the part. So, your script would be:

local debounce = true

script.Parent.Touched:Connect(function(partThatTouched)
    local humanoid = partThatTouched.Parent:FindFirstChildWhichIsA("Humanoid") --Humanoid
    if humanoid then --Checks for humanoid
        local player = game.Players:FindFirstChild(partThatTouched.Parent.Name) --Finds player
        local plrstats = player.leaderstats.Level
        if debounce then
            debounce = false
            plrstats.Value = 1
            wait (1)
            debounce = true
        end
    end 
end)
0
Thanks yous very much Rynappel 212 — 5y
0
Welcome! HomieFirePGN 137 — 5y
Ad

Answer this question