So I set up cash leaderstats in my game, the leaderstats work perfectly fine but whenever I try to reference leaderstats in a script it errors out saying "attempt to index nil with 'leaderstats'"

Script used to setup leaderstats:

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 -- this script works as intended

game.Players.PlayerAdded:Connect(onPlayerJoin)

this is the script that is showing the error:

local Players = game:GetService("Players")
local player = Players.LocalPlayer

local cash = player.leaderstats.Cash.Value

any ideas?

4 comments

0

That's because you cannot use a local player when trying to change things such as leaderstats e.t.c

0

Also what are you trying to do on line 4

0

changing the name of the folder

0

If you are trying to change anything then you need to use remote events instead of local unless you have filteringenabled turned off