So I have been working on this for a while but I can't figure out how to get it to work without the erroring out.
here is the script that sets up a leaderboard:
local function onPlayerJoin(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local cash = Instance.new("IntValue")
cash.Name = "Cash"
cash.Value = 10
cash.Parent = leaderstats
end
game.Players.PlayerAdded:Connect(onPlayerJoin)
that script works perfectly fine and the leaderboard works as intended, but when whatever I do here does not work, it always errors out saying:
attempt to index nil with 'leaderstats'
these are two different scripts btw
local Players = game:GetService("Players")
local function onCharacterAdded(character)
local cash = character.leaderstats.Cash.Value -- THIS is the line that is showing the error
return cash
end
local function onPlayerAdded(player)
player.CharacterAdded:Connect(onCharacterAdded)
end
Players.PlayerAdded:Connect(onPlayerAdded)
local cash = onCharacterAdded()
if you have any ideas on how I could fix this let me know

1 comment
Thats because you are referencing the parameter character to leaderstats make sure you referenced userdata like game.Workspace.leaderstats.Cash.Value