Can't Find Tool in Backpack with LocalScript

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?
    I am trying to make a local script that can handle a player’s tools.
  2. What is the issue?
    The issue is that however I try to access tools from a player’s backpack, it will return nil. It also says that there is nothing inside the player’s backpack when I GetChildren().
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have tried using WaitForChild, FindFirstChild, wait(), I’ve found this dev forum: Script doesn't find tool in backpack

but it doesn’t solve my problems.
This script is far from complete, but I was hoping that it could at least find a tool. Here’s the script:

local player = game.Players.LocalPlayer

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PickInfo = require(ReplicatedStorage.PickInfo)

local activeTool = Instance.new("StringValue")
activeTool.Name = "ActiveTool"
activeTool.Value = "None"

local Backpack = player:WaitForChild("Backpack")
print(Backpack, Backpack.Parent)
wait(5)
print(Backpack:GetChildren())
local mainTool = Backpack:FindFirstChild("BasicPick")

local toolDebounce = false

mainTool.Activated:Connect(function()
	if toolDebounce == false then
		toolDebounce = true
		print("nice")
		activeTool.Value = mainTool.Name
		
		wait(PickInfo[mainTool].Cooldown)
		toolDebounce = false
	end
end)

I’ve tried to debug it a bit, but it doesn’t work. Both WaitForChild() and FindFirstChild() don’t work as expected.

2 Likes

Can you elaborate on “that can handle a player’s tools.”?

1 Like

Basically It would check if a tool has been clicked, update a string value to the tool’s name which allows a server side script to know what tool has been used. I don’t have big plans for it yet, but I sort of have to get pass this step for anything else to happen.

And just noting that Im trying to have one local script handle all the tools, but for now It will only handle the main tool.

Maybe try looking at these -

Tool Activated
Tool Reference
Getting A Players Tool

The problem could be the tool Activation, but.

Ok, I’ll try out the function in Getting A Players Tool. But I’m positive it doesn’t have to do with the tool activation because I can’t even find the tool yet.

Lets break down multiple issues in your code.

You haven’t parented this to anything, I’m not sure if this is intentional or not.

GetChildren() returns a table and will not print an actual string.

Your updating a value on the client, the server will not see the updated value. You must use remote events.

You’re not actually taking into consideration that a tool could be added at a later time, plus you taking the wrong approach on how to do this.

Oh ok, that might be the problem, thanks. I’ll first focus my attention on getting the tool though. I’ll try to fix some of the problems .

If you want to get all the tools in your backpack you should use the example provided. Keep in mind this will only work for tools in the backpack when it runs the check. You can add a wait.

Example
for i,v in pairs(PlayerObject.Backpack:GetChildren()) do
    if v:IsA("Tool") then
        v.Activated:Connect(function()
            --//Code here
        end)
    end
end

Thanks, I’ll try that out. I just fixed the first two problems you mentioned above. I parented the StringValue activeTool to player, and also used a for loop to print the names of the children of the backpack, and printed the length of it, but it doesn’t print anything out, and it says the table length is 0.

Also, I forgot to mention. The activeTool value is created on a localscript. So it doesn’t exist on the server. You should probably use the PlayedAdded event to create the value when the person joins on the server.

Yeah I’m going to create a separate script for that later on. I’ve tried implementing the script you provided and it didn’t do anything.

local player = game.Players.LocalPlayer

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PickInfo = require(ReplicatedStorage.PickInfo)

local activeTool = Instance.new("StringValue")
activeTool.Parent = player
activeTool.Name = "ActiveTool"
activeTool.Value = "None"

local Backpack = player:WaitForChild("Backpack")
print(Backpack, Backpack.Parent)
wait(5)

for i,v in pairs(player.Backpack:GetChildren()) do
	if v:IsA("Tool") then
		v.Activated:Connect(function()
			print("nice")
		end)
	end
end

not to mention the print(Backpack, Backpack.Parent), prints “Backpack, nil” which doesn’t make sense because if it has gotten Backpack, then theoretically the player/parent of the backpack has to exist right?

Your printing this before the wait. The player may have not loaded nor the backpack.

the print is after the WaitForChild(“Backpack”)

Can you screenshot your console after testing the game and equipping a tool and activating it, keep in mind you must wait 5 seconds before equipping and testing a tool. Since your doing :WaitForChild(“Backpack”) the 5 seconds seems a bit long, maybe adjust it to 1.

Ok im trying to send it, but I have to switch to a different device to send it for some reason, and the Roblox login is getting a bit annoying.

1 Like

By any chance do you know why devforum is saying access denied when I click login on a separate device

Are you on a windows or mac device?

Edits:

Mac Screenshot - Command + Shift + 4 (Then select the area you want to screenshot)
Windows - WindowsButton + PrntScr (Full screen shot only)

I believe there is a built in screenshot option on Roblox studio under the view tab

Oh for **** sake. Im on Mac, but I just realized that the devforum isn’t for ages <13 right? and my account got hacked a week+ ago and they changed my birthday to 2015, and roblox never changed it back. So now I can’t use devforum on a seperate device again.

No, my browsers on Mac don’t give me any popup when I try to click the choose file options

Hold Command and Shift and 3 for a fullscreen screenshot, it will save to your desktop.