Attempt to index nil with leaderstats

I created a tool which shoots a bomb in the position of the cursor
The script is supposed to give exp from the line
game.Players.LocalPlayer.leaderstats2.XP.Value = game.Players.LocalPlayer.leaderstats2.XP.Value + 10

However instead it gives an error image

Is their a way to fix this?
Thank you for your time

8 Likes

Players.LocalPlayer is nil to the server, so you need to get it another way, like through Players.PlayerAdded.

8 Likes

Is this in a LocalScript? If it isn’t, you cannot get the LocalPlayer.

6 Likes

It’s not a local script sorry I didn’t realize is their an alternative way to get the player?

2 Likes

For your situation, the following should work: (judging by your error)

local player = script.Parent.Parent.Parent --One parent is the tool, another parent is the backpack, the third parent is the player
--and then do your stuff....
player.leaderstats2........
2 Likes

I tried the way you did it however I got two errors. This is the first error image and the second one is image It happens when the player unequips his weapon.

2 Likes

Alright, the first error is caused because you save data too often (I don’t have the script so I can’t say much more).

The second error I don’t have any code for, so how can I help?

Those errors are unrelated to the first bit, so I can’t help you. Could you please provide those scripts?

3 Likes

Your trying to make Backpack a member of Players when Players is a service not the actual player. [Second Error]

3 Likes

Im having a similar situation, I tried using Players.PlayerAdded but i am given this error:

image

This is my script:

while true do

wait(3)

script.Parent.TextLabel.Text = "Level".. game.Players.PlayerAdded.leaderstats.Level.Value

end

(I am having trouble displaying a players level on a billboardgui)

3 Likes

player added is an event which you can do stuff such as

game.players.PlayerAdded:connect(function()
--what ever your code is
end)

instead what you need to do is use

game.players.YourPlayer.leaderstats.Level.Value
2 Likes