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

Points on a leaderboard?

Asked by 6 years ago
Edited 6 years ago

So I'm quite new to lua, but I've had some experience. I came across this

game.Players.PlayerAdded:connect(function(player)
    stats = Instance.new("IntValue", player)
    stats.Name = "leaderstats"

    points = Instance.new("IntValue", stats)
    points.Name = "Points"
    points.Value = 0
end)

script.Parent.Part.Touched:connect(function(hit)
    for i, v in pairs(game.Players:GetPlayers()) do

        if v == hit.Parent then
            points.Value = points.Value + 10
        end
    end
end)

It's supposed to add points to the leaderboard when each player touches a part. So in other words, it loops around all the players that are in the server, and if it's the right player, it gives him 10 points. But it doesn't seem to add points. Did I do something wrong? And if there's something that could be done to make it better, I'd be happy to know.

Regards, Techadox.

2

1 answer

Log in to vote
1
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
6 years ago

Ok..

  • You never defined points in the Touched event.

  • You should use the GetPlayerFromCharacter function to get the Player object.

  • Also, I suggest adding a Debounce, to prevent the code from bounce-firing.

local db = false --Debounce, default off.

script.Parent.Part.Touched:connect(function(hit)
    if not db then --if debounce is off, execute code
        db = true --turn on debounce
        local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
        if plr then --Check if it's a player that touched
            local points = plr:FindFirstChild("Points") --find value
            if points then --check if it exists
                points.Value = points.Value + 10 --increment value
            end
        end
        db = false --turn off debounce
    end
end)
Ad

Answer this question