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

Whats wrong with this onTouch?

Asked by 9 years ago
script.Parent.Touched:connect(function()
    for _,Player in pairs(game.Players:GetPlayers()) do
        if Player:FindFirstChild("leaderstats") then
            Player.leaderstats.Wins.Value = Player.leaderstats.Wins.Value +1
        end
    end
end)

What it will do is when you touch it, it will add 1 to the Player who touches its Wins. But it adds about 3

Also, I want to have it when you touch it to ALSO teleport the player to the lobby... problem being how? I know you have to CFrame their BodyRootPart but how would I find the player who touched it and CFrametheir BodyRootPart?

2 answers

Log in to vote
3
Answered by 9 years ago

The reason it was adding more than 1 point for touching it is because you had nothing stopping the script running again. This should work, but with your script it would have added points to every player in the game, so use this:

db = false -- Create a delay variable

script.Parent.Touched:connect(function(hit)
    if db == false then -- Checks if it's been recently used
        db = true -- Stops players from using it agian
        for i,v in pairs(game.Players:GetChildren()) do
            if v.Name == hit.Parent.Name then
                if v:FindFirstChild("leaderstats") then
                    v.leaderstats.Wins.Value = v.leaderstats.Wins.Value + 1
                    wait(10) -- Waits before it can be used again
                    db = false -- Allows players to use it again
                end
            end
        end
    end
end)
0
Updated it when I read about it adding more than 1 win. It now has a 10 second delay before it can be used again! General_Scripter 425 — 9y
0
Thanks! ggggyourface23 63 — 9y
Ad
Log in to vote
0
Answered by 9 years ago
script.Parent.Touched:connect(function(h)
    if h.Parent:FindFirstChild("Humanoid") then
        local player = game.Players:FindFirstChild(h.Parent.Name)
        if player then
            player.leaderstats.Wins.Value = player.leaderstats.Wins.Value + 1
        end
    end
end)

Answer this question