Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

[Resolved] Why doesn't the tool giver give the tool?

Asked by
SWX253 2
4 years ago
Edited 4 years ago

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:

01local PickaxeGiver = script.Parent
02local Pickaxe = game.ReplicatedStorage.GameItems:WaitForChild("Pickaxe")
03local ClonedPick = game.ReplicatedStorage.GameItems.Pickaxe:Clone()
04 
05PickaxeGiver.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
14end)

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.

0
should i help you in studio real quick? My roblox name is esepek. esepek 103 — 4y
0
It doesn't know what you are assigning for "Backpack" meaning you probably need to change plr.Backpack to something else FBS_8 25 — 4y

1 answer

Log in to vote
0
Answered by
Leamir 3138 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago
01local PickaxeGiver = script.Parent
02local Pickaxe = game.ReplicatedStorage.GameItems:WaitForChild("Pickaxe")
03local ClonedPick = game.ReplicatedStorage.GameItems.Pickaxe:Clone()
04 
05PickaxeGiver.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
15end)

Note: this will only make 1 player to be able to have the pickaxe

0
Mark this as correct Nckripted 580 — 4y
0
Imma test it out before marking it correct SWX253 2 — 4y
0
Note: To make this compatible for multiple people, clone the pickaxe when it sets the parent instead of cloning it at the top of the script. xAtom_ik 574 — 4y
0
Thx for ur help SWX253 2 — 4y
Ad

Answer this question