-
What do you want to achieve?
I want to give a sword to the player upon touching a part, but check to see if they have a sword first. If they do, then don’t give them the sword. -
What is the issue?
" Workspace.Part.Script:9: attempt to index nil with ‘Backpack’ "
Here is the script:
script.Parent.Touched:Connect(function(hit)
local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
if player then
if player.Backpack:FindFirstChild("ClassicSword") == nil then --this is line 4
end
else
local sword = game.ReplicatedStorage.ClassicSword
local swordClone = sword:Clone()
swordClone.Parent = player.Backpack --this is line nine
end
end)
I’m not sure what is wrong with line nine. I suspect that something goes wrong in the beginning of the “if” statement but I’m not sure.
3. What solutions have you tried so far?
Well the original script didn’t have “== nil” on line 4, so I thought that was the issue at first. But I added that and it still isn’t working. I looked at similar posts to mine on the dev forum but all of them had different issues than mine, whether it be the wrong script type, not defining player, etc. I didn’t see my specific issue in them. I might be overlooking something really obvious though.