Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Leader board script not working correcly?

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

✓ Accepted answer

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.