Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How should i get all the tools the player has? [closed]

Asked by 4 years ago

I'm trying to create a custom tool gui and i am stuck at how i should get the tools I've tried using Backpack:GetChildren() but it was really bad because when i equipped a tool it would disappear I need some help with getting all of the tools as objects (if they are equipped or not) in a array

Closed as Not Constructive by youtubemasterWOW and JesseSong

This question has been closed because it is not constructive to others or the asker. Most commonly, questions that are requests with no attempt from the asker to solve their problem will fall into this category.

Why was this question closed?

3 answers

Log in to vote
1
Answered by 4 years ago

When tools are equipped, they are parented to the player's character model, meaning we would have to check their character for any tools as well.

local player = --path to player

for i,v in pairs(player.BackPack:GetChildren()) do
    if v:IsA("Tool") then
        --v is a tool
    end
end

for i,v in pairs(player.Character:GetChildren()) do
    if v:IsA("Tool") then
        --v is a tool
    end
end
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

Hello. When tools get equipped, they get placed in the player's character, a descendant of Workspace. So, instead of just searching the player's backpack, search the player's character for a tool. Try this:

local player = game:GetService("Players").LocalPlayer
local backpack = player:WaitForChild("Backpack")

local function checkForTools()
    local character = player.Character or player.CharacterAdded:Wait()

    local tools = backpack:GetChildren()

    for i, v in pairs(character:GetChildren()) do
        if v:IsA("Tool") then
            table.insert(tools, 1, v)
        end
    end

    return tools
end

local tools = checkForTools()

When the function is called, it will make a variable called tools which are the tools in the backpack. Then, I used an in pairs loop to loop through the player's character. If there was a tool in there, it would insert it into the tools array. Finally, the function returns the tools as an array. Note that I'm using a LocalScript. You can also use a ServerScript, but make sure you change the player variable to the player via RBXScriptSignal (PlayerAdded, ClickDetector, :GetPlayerFromCharacter(), etc.)

Please accept and upvote this answer if it helped.

Log in to vote
0
Answered by
crywink 419 Moderation Voter
4 years ago
Edited 4 years ago

Hey!

When a tool is equipped, it needs to be shown to the world somehow, right? It can't do this from the backpack, so when you equip a tool, it's parented to the character (it needs to be a descendant of Workspace). If you wanted to get all of the tools, equipped or not, you can create a function that'll grab all the tools from your backpack and grab the equipped tool, if there is one. I've provided an example of this function below...

local Player = game.Players.LocalPlayer
local Character = Player.Character

local function GetAllTools()
    local Tools = {} -- This is where we'll put all of the tools; it'll be returned at the end.

    for _,v in pairs(Player.Backpack:GetChildren()) do
        if v:IsA("Tool") then -- We'll make sure it's a tool because sometimes people like to parent other things to the backpack
            table.insert(Tools, v)
        end
    end

    local EquippedTool = Character:FindFirstChildOfClass("Tool") -- This will find any tool parented to the character
    if EquippedTool then
        table.insert(Tools, EquippedTool)
    end

    return Tools
end

From here, we just call our GetAllTools() function and it'll return all of the tools, equipped and unequipped!

If you only wanted to get the currently equipped tool, you could just use FirstFirstChildOfClass to see if there's a tool in the character like so...

local EquippedTool = Character:FindFirstChildOfClass("Tool")
if EquippedTool then
    print("Player has", EquippedTool, "equipped!")
else
    print("Player has nothing equipped")
end

And lastly, if you just wanted to get the tools that you don't have equipped, you can just use Player.Backpack:GetChildren() and it'll return an array with just the tools that are in your backpack currently.

Hope this helped! If it did, please remember to mark as answered and upvote.