Attempt to index nil with leaderstats

So I’ve had this error on my script, and it’s ‘attempt to index nil with leaderstats’

I’ve read around but none of the posts have been at all helpful, and I was wondering if anyone could help!

My script:

local debounce = false
script.Parent.Touched:Connect(function(hit)
	print("Check1")
	local player = hit.Parent
	local gameplayer = game.Players:GetPlayerFromCharacter(player)
	if not debounce then
		debounce = true
		wait(3) ---- How long till you collect cash (Seconds)
		print("Check2")
		gameplayer.leaderstats.Money.Value = gameplayer.leaderstats.Money.Value + 4000---- Money to add
		print("Check 3")
		wait(10)
		debounce = false
	end	
end)

Can anyone tell me where I’ve gone wrong please?

(Leaderstats Script:)

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Money = Instance.new("IntValue")
	Money.Name = "Money"
	Money.Parent = leaderstats
end)

Check if the Object you’re hitting is actually the Player and not just some random Object

local debounce = false
script.Parent.Touched:Connect(function(hit)
	print("Check1")
	local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if not debounce and Player then -- if Debounce is false and player isnt nil
		debounce = true
		print("Check2")
		gameplayer.leaderstats.Money.Value += 4000---- Money to add
		task.wait(13)
		print("Check 3")
		debounce = false
	end	
end)

Change:

To local leaderstats = Instance.new("Folder",player)

Remove the 4th Line.

Also, IntValues can’t be added in Numbers so it should be like this:

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder",player)
	leaderstats.Name = "leaderstats"
	
	local Money = Instance.new("NumberValue",leaderstats)
	Money.Name = "Money"
	Money.Value = 0
end)

To make it function, add:

local Data = game:GetService("DataStoreService"):GetDataStore:("TheRandomData")

And do this at the End so It Can Save the Stats:

game.Players.PlayerRemoving:Connect(function(plr)
	Data:SetAsync(tostring(plr.UserId),{
		plr.leaderstats.Money.Value,
	})
end)

But look… (I’m abusing the char limit)

Ok, but i’ve already set up something else via this. Will it change it?

That makes things worse. The original code is actually better.
Using Instance.new with a parent argument is considered code smell.

No it makes it worse. I recommend changing it back to the way you did it originally.

1 Like

I haven’t changed it… (Character limit abusing)

What’s the solution then? Cause i’ve tried everyhting…

What about it? you are only getting the Detected parts Parent, it could be anything, you appear to be getting the Player from its character, but it may not be its character, thats why im suggesting to check if its actually the player

oops, forgot to include the next line

this is all im saying to do:

local gameplayer = game.Players:GetPlayerFromCharacter(player)
if not Debounce and gameplayer then
1 Like

will that solve all my issues and still make everything work?

What this will do is check if its actually the Player, when getting the Player’s Character, if an Objects Parent isnt a Player, it will return nil. but here the if statement is checking if the object touching Is a Player or not

1 Like

Ok. Can I ask you abt one more thing please?

1 Like

You dont have to ask, just say it!

So basically, I’ve been trying to make a GUI change text every 2.5 seconds for example.

The gui pops up when you touch a part, and stays for 100 seconds, and it should start counting up in 200s till 4000.

I have been able to get the gui to pop up but i can’t seem to change the text on the gui. Let me find the script and i’ll send it

Also now i just keep getting the rendering for debugging screen!!!


image
Should i be worried ^^^

Check if you have a Breakpoint:

Screenshot (127)
The Breakpoint is that red circle

image