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:

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.

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
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

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