The script is supposed to put the pickaxe in the player's backpack when they touch the pickaxe giver. Unfortunately, when I testplay it, it gives this error at line 8:
Attempt to index nil with Backpack
The script:
local PickaxeGiver = script.Parent local Pickaxe = game.ReplicatedStorage.GameItems:WaitForChild("Pickaxe") local ClonedPick = game.ReplicatedStorage.GameItems.Pickaxe:Clone() PickaxeGiver.Touched:Connect(function(hit, plr) if hit.Parent:FindFirstChild("Humanoid") then print("Check Works") if Pickaxe.Parent ~= plr.Backpack then print("Tool Check Works") ClonedPick.Parent = plr.Backpack print("Give Tool Works") end end end)
I've looked at other posts to no avail. I've tried splitting the function into two separate functions and running it then. Tips would be appreciated since I'm relatively new scripter.
local PickaxeGiver = script.Parent local Pickaxe = game.ReplicatedStorage.GameItems:WaitForChild("Pickaxe") local ClonedPick = game.ReplicatedStorage.GameItems.Pickaxe:Clone() PickaxeGiver.Touched:Connect(function(hit) -- you can't define plr here... local plr = game.Players:GetPlayerFromCharacter(hit.Parent) --Here we attempt to get player instance from the parent of the part that touched. if plr then -- here we check if its a valid player print("Check Works") if Pickaxe.Parent ~= plr.Backpack then print("Tool Check Works") ClonedPick.Parent = plr.Backpack print("Give Tool Works") end end end)
Note: this will only make 1 player to be able to have the pickaxe