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.
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.
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)
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.
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.
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
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.
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)
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?