Having trouble making a Tool give me Another Tool

I would like to make it so when you click with the box tool, the box will delete itself, while giving you another tool.

Whenever I start the game, and use the tool, it gives me this error saying: attempt to index nil with 'Backpack'. However, when I use the same script but with clicking a part in the workspace, it works just fine! They’re both regular scripts as well.

Screen Shot 2020-11-26 at 7.04.08 PM

I’m not a good scripter, so I used @Nimblz script for giving a tool to the player, and switched the function to work with script.Parent.Activated:Connect(boop) instead of the ClickDetector.MouseClick. I did a search for this problem, and didn’t find anything that would help me with this issue.

Below Is the script I’m using, and it keeps printing that error above. I’m not using a LocalScript because if I used Local, I believe it’d only show for the LocalPlayer, and not the other people.

Toolname = "Crowbar"

local function boop(Player)
	if not Player.Backpack:FindFirstChild(Toolname) then
		local Tool = game.ServerStorage[Toolname]:clone()
		Tool.Parent = Player.Backpack
	end
wait(0.5)
	script.Parent.Handle:Destroy()
end

script.Parent.Activated:Connect(boop)

Any help would be fantastic! Not sure what I’m doing wrong here, because this process works perfectly fine with ClickDetectors, but not tools.

-Void_Xiety :white_heart:

Looking at the documentation it says that on the server you will need to find the player name to access that person’s backpack like so:

--Accessing Backpack from a Server Script:
game.Players.PlayerName.Backpack

Thing is click detectors automatically give the specific player instance on the mouseclick event which tools don’t.

And unfortunately we can’t use .LocalPlayer as its only for local scripts. Consequently, you will need to find another way to find the specific player instance perhaps from the player name who is the Parent of the tool.

1 Like

I’ll look through the documentation and let you know if I do find a solution. If I encounter a different error while doing so, I’ll post here again.

This should work using this property of tools when it’s equipped from the documentation.

Equipping a tool moves it from the Backpack and into a player’s Character|character model in the Workspace

--Define constants and services which is good practice
--Also yeah use the local keyword for optimization but that's
--a different topic which you can search online for reasons why

local Players = game:GetService("Players")
local currentTool = script.Parent
local Toolname = "Crowbar"

local function boop(Player)
--Do the finding the specific player who equipped the tool
	local character = currentTool.Parent
	local playerWhoEquippedTool = Players:GetPlayerFromCharacter(character)

	if not playerWhoEquippedTool.Backpack:FindFirstChild(Toolname) then
		local Tool = game.ServerStorage[Toolname]:clone()
		Tool.Parent = playerWhoEquippedTool.Backpack
	end
	wait(0.5)
	script.Parent.Handle:Destroy()
end

currentTool.Activated:Connect(boop)
1 Like

Thank you so much! I think it’s the below that I had missed.

This script has seemed to fix the problem. Thank you lots! I’m sure this will help others as well.

I will try and use GetPlayerFromCharacter() more often.

1 Like