Attempt to index nil with ‘leaderstats’?

  1. What do you want to achieve?

I would like the tool to be removed from the player & the player’s money leaderstats to increase by 25. I have two separate scripts to accomplish each—but they’re messing with each other for some reason.

  1. What is the issue?

I’ve continued to get this error when the tool touches the part (aka NPC):

Workspace.NPCs.NPC.UpperTorso.MoneyScript:8: attempt to index nil with ‘leaderstats’

I’m not sure what it means but I’ve tried rewriting both scripts in order for it to work; however, it seems like whatever I do doesn’t work. I’m very new to scripting so I apologize if my scripts aren’t good.

  1. What solutions have you tried so far?

Rewriting my scripts & looking at DevForum posts.

More Information:

Both ServerScripts are within the ‘UpperTorso’ of the NPC.

MoneyScript:

local giver = game.Workspace.BearP
local prp = script.Parent
local customer = prp.Parent

script.Parent.Touched:Connect(function(Hit)
	if Hit.Parent.Name == 'Bear' and game.StarterPlayer["BearV"].Value == true then
		game.Players:GetPlayerFromCharacter(Hit.Parent.Parent).leaderstats.Money.Value += 25
		game.StarterPlayer["BearV"].Value = false

	end
end)

RemoveToolScript:

script.Parent.Touched:Connect(function(Hit)
	local player = game.Players:GetPlayerFromCharacter(Hit.Parent.Parent)
	if Hit.Parent.Name == 'Bear' then
		local backpack = player.Backpack

		local char = player.Character
		if not char then return end

		local hum = char.Humanoid
		hum:UnequipTools()
		wait()

		backpack:FindFirstChild("Bear"):Destroy()
	end
end)

Screen Shot 2022-07-21 at 8.43.29 PM

I don’t know if this was why but,
Instead of:
:GetPlayerFromCharacter(Hit.Parent.Parent)
You can try
:GetPlayerFromCharacter(Hit.Parent.Parent.Parent)

You can keep added “.Parent” or taking away “.Parent” until it hopefully works! :cool:

1 Like

I’ve tried adding 6 ".Parent"s and it hasn’t worked. I’ve also tried [“leaderstats”].Money.Value and [“leaderstats”][“Money].Value” and that doesn’t work either.

I’m pretty sure it’s saying that the player is nil… but I’m not sure why cause Hit.Parent = the tool and Hit.Parent.Parent = the player?

What is even more frustrating is that the script(s) works perfectly fine within another NPC & it’s the SAME script(s) except with a different tool & BoolValues.

Hi.

You’ve got a conditional in your hit function that passes to the next line if it detects the Bear NPC.

Within that conditional, you’re trying to detect the leaderstats using the :GetPlayerFromCharacter() method.

However, you can’t do this, since you’ve put Hit.Parent.Parent, aka the NPC. :GetPlayerFromCharacter() only works when the path inside the () leads to a player’s model, not an NPC.

1 Like

For readability, do not make a .Parent chain when trying to find a character model to determine if it’s a player or not. Use :FindFirstAncestorOfClass(“Model”) instead

local character = hit:FindFirstAncestorOfClass("Model")

if game.Players:GetPlayerFromCharacter(character) then
   local player = game.Players:GetPlayerFromCharacter(character)
   --do things
end

Also for the moneyscript, do not use StarterPlayer to access things for live changes, use the player’s character

1 Like

How should I create a path that would lead to the player’s model?

:GetPlayerFromCharacter(Hit.Parent.Parent.Parent.Players.LocalPlayer)

?

Would this be inserted into the Money Script or the RemoveTool Script?
Also, I’ve placed the BoolValue’s into StarterPlayer which is why I’ve done that.

In all the scripts that are using the :GetPlayerFromCharacter function

I’ve tried inserting the script you’ve provided to me within both scripts; however, now the tool isn’t removed from the Player & the leaderstats value doesn’t increase.

MoneyScript:

local giver = game.Workspace.BearP
local prp = script.Parent
local customer = prp.Parent

script.Parent.Touched:Connect(function(Hit)
local character = Hit:FindFirstAncestorOfClass("Model")
if game.Players:GetPlayerFromCharacter(character) then
   local player = game.Players:GetPlayerFromCharacter(character)

	if Hit.Parent.Name == 'Bear' and game.StarterPlayer["BearV"].Value == true then
		game.Players:GetPlayerFromCharacter(Hit.Parent.Parent).leaderstats.Money.Value += 25
		game.StarterPlayer["BearV"].Value = false
end
	end
end)

RemoveToolScript:

script.Parent.Touched:Connect(function(Hit)
local character = Hit:FindFirstAncestorOfClass("Model")
if game.Players:GetPlayerFromCharacter(character) then
   local player = game.Players:GetPlayerFromCharacter(character)

	if Hit.Parent.Name == 'Bear' then
		local backpack = player.Backpack

		local char = player.Character
		if not char then return end

		local hum = char.Humanoid
		hum:UnequipTools()
		wait()

		backpack:FindFirstChild("Bear"):Destroy()
end
	end
end)

You should try looking for the BearV values in the player, as starterplayer is just a replication service

Try debugging it with prints, to see what line its stopping at.

Would the BearV value be within the Player though? I tried to search for the value within the player & I couldn’t find it; however, it was in the StarterPlayer folder. Is that what you meant by putting the BearV BoolValue into StarterCharacterScripts? :sweat_smile:

LemonElk1113179 helped me resolve the issue here: How to destroy tool in Player's Inventory/Backpack? - #24 by Auevi :slight_smile:

Thank you everyone for your time and assistance!

1 Like