You can write your topic however you want, but you need to answer these questions:
What do you want to achieve?
I am trying to make a local script that can handle a player’s tools.
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().
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.
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.
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.
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?
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.
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.