Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

Leader board script not working correcly?

Asked by 1 year ago

I am making a leaderboard for a game which displays a players rank in a group, and displays the amount of cash a player has. But it does not work. Please fix it because i suck at scripting.

game.Players.PlayerAdded:connect(function(player)
local playerLeaderstats = {} 
local leaderstats = Instance.new("IntValue")
leaderstats.Name = "leaderstats"
leaderstats.Value = 0

local rank = Instance.new("StringValue")
rank.Name = "Rank"
rank.Value = player:GetRoleInGroup(3309505) 


leaderstats.Parent = player
rank.Parent = leaderstats

game.Players.PlayerAdded:connect(function(player)
    playerLeaderstats[player] = {}
    playerLeaderstats[player]["Cash"] = 100
    local leaderstats = Instance.new("Model")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player
    local money = Instance.new("IntValue")
    money.Name = "Cash"
    money.Value = playerLeaderstats[player]["Cash"]
    money.Parent = leaderstats
end)

while wait(5) do 
    for _, player in pairs(game.Players:GetPlayers()) do
        playerLeaderstats[player]["Cash"] = playerLeaderstats[player]["Cash"] + 1
        if player:FindFirstChild("leaderstats") then
           player.leaderstats.Money.Value = playerLeaderstats[player]["Cash"]
        end
    end
end

end)

1 answer

Log in to vote
1
Answered by 1 year ago
Edited 1 year ago

Here is a correctly version on your script, you have not need to put two "PlayerAdded" signal.

local playerLeaderstats = {}

game.Players.PlayerAdded:connect(function(player)

    local leaderstats = Instance.new("Folder", player)
    leaderstats.Name = "leaderstats"

    local rank = Instance.new("StringValue", leaderstats)
    rank.Name = "Rank"
    rank.Value = player:GetRoleInGroup(3309505) 

    playerLeaderstats[player] = {}
    playerLeaderstats[player]["Cash"] = 100

    local money = Instance.new("IntValue", leaderstats)
    money.Name = "Cash"
    money.Value = playerLeaderstats[player]["Cash"]

    while wait(5) do 
        playerLeaderstats[player]["Cash"] = playerLeaderstats[player]["Cash"] + 1
        if player:FindFirstChild("leaderstats") ~= nil then
            player.leaderstats.Cash.Value = playerLeaderstats[player]["Cash"]
        end
    end

end)
Ad

Answer this question