Attempt to index nil with 'leaderstats' Error Message

  1. What do you want to achieve? I want it to be so when the player joins a timer starts for every 1 second and gives the player 1 extra coin.

  2. What is the issue? It tells me attempt to index nil with ‘leaderstats’

  3. What solutions have you tried so far? I have looked on the dev forum and found some posts related but not totally helpful.

  4. Extra Information. I got the server to transfer over to the local script fine but its, when it does is when it has an error.
    Here are the leaderstats server side.

 game.Players.PlayerAdded:Connect(function(player)
	local folder = Instance.new('Folder',player)
	folder.Name = "leaderstats"
	
	local coins = Instance.new("IntValue", folder)		
	coins.Name = "Coins"
	coins.Value = 0
end)

Here is the CoinsServer.

game.Players.PlayerAdded:Connect(function(player)
	local Iplayer = player
	while wait(1) do
		game.ReplicatedStorage.RemoteEvents.SecondCoins:FireClient(Iplayer)
	end
end)

Here is the CoinsClient.

game.ReplicatedStorage.RemoteEvents.SecondCoins.OnClientEvent:Connect(function(player)
	player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 1
end)

The problem is local script, you didn’t get the player :smiley:
Here is working script:

local plr = game.Players.LocalPlayer

game.ReplicatedStorage.RemoteEvents.SecondCoins.OnClientEvent:Connect(function()

plr.leaderstats.Coins.Value += 1

end)

The issue is that you’re not passing the player object to the client. The first argument for FireClient should be the player that you want to send data to, followed by whatever data you want to send to that player, e.g:

Remote:FireClient(player, "hello", "world")
Remote.OnClientEvent:Connect(function(arg1, arg2)
    print(arg1, arg2) -- > hello world
end)

So if you want to pass the player to change the coins for, you would do

game.ReplicatedStorage.RemoteEvents.SecondCoins:FireClient(Iplayer, Iplayer)

PS, you should probably be setting the coins on the server, not the client.

Well do you know the difference between Client and Server?

First, you are giving player coins through Server to Client instead of Client to Server.
So player will get coins but, it will change only for the the local player that is you.

Link if you want to learn more: Difference between server and client - Stack Overflow.

Now time to fix your script.
Your leaderstats script is right.

Instead of firing the event your can do in script itself.

game.Players.PlayerAdded:Connect(function(player)
	local Iplayer = player
	while wait(1) do
        if player:FindFirstChild("leaderstats") then
              player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 1
          else
             local lead = player:WaitForChild("leaderstats"):WaitForChild("Coins")
            player[lead].Value = player[lead].Value + 1
        end
	end
end)

I’ve tested it and its working for me :smiley:
Thanks have a great day/night !

Thanks so much I was told before that when you pass remote events through a local script it automatically gets the player.