Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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.

2 comments

0

should i help you in studio real quick? My roblox name is esepek.

0

It doesn't know what you are assigning for "Backpack" meaning you probably need to change plr.Backpack to something else

1 answer

✓ Accepted answer
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

4 comments

0

Mark this as correct

0

Imma test it out before marking it correct

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.

0

Thx for ur help

This discussion is preserved as it was. New answers, comments, and votes are closed.