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:
01 | local PickaxeGiver = script.Parent |
02 | local Pickaxe = game.ReplicatedStorage.GameItems:WaitForChild( "Pickaxe" ) |
03 | local ClonedPick = game.ReplicatedStorage.GameItems.Pickaxe:Clone() |
04 |
05 | PickaxeGiver.Touched:Connect( function (hit, plr) |
06 | if hit.Parent:FindFirstChild( "Humanoid" ) then |
07 | print ( "Check Works" ) |
08 | if Pickaxe.Parent ~ = plr.Backpack then |
09 | print ( "Tool Check Works" ) |
10 | ClonedPick.Parent = plr.Backpack |
11 | print ( "Give Tool Works" ) |
12 | end |
13 | end |
14 | 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.
01 | local PickaxeGiver = script.Parent |
02 | local Pickaxe = game.ReplicatedStorage.GameItems:WaitForChild( "Pickaxe" ) |
03 | local ClonedPick = game.ReplicatedStorage.GameItems.Pickaxe:Clone() |
04 |
05 | PickaxeGiver.Touched:Connect( function (hit) -- you can't define plr here... |
06 | local plr = game.Players:GetPlayerFromCharacter(hit.Parent) --Here we attempt to get player instance from the parent of the part that touched. |
07 | if plr then -- here we check if its a valid player |
08 | print ( "Check Works" ) |
09 | if Pickaxe.Parent ~ = plr.Backpack then |
10 | print ( "Tool Check Works" ) |
11 | ClonedPick.Parent = plr.Backpack |
12 | print ( "Give Tool Works" ) |
13 | end |
14 | end |
15 | end ) |
Note: this will only make 1 player to be able to have the pickaxe