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

How would you find the current equipped tool?

Asked by
Scaii_0 145
5 years ago
Edited 5 years ago

I need to get to whatever tool the player has equipped currently. I know that when a player equips a tool, it goes to the character. So how would I get to that tool?

I have thought of trying to find the first child that is a tool. I don't know how to do this yet and I don't know if it is very efficient as it would break when another tool is added.

0
You should post your attempt. It helps not only us, but yourself too. It's okay to ask for help. If you have a question on how to do something and you have an attempt, post it. . User#19524 175 — 5y
0
I haven't attempted it, I have no clue on what functions I would use to do this. Scaii_0 145 — 5y
0
Are you looking for a list of tools the player has equipped? If that's the case then you'd want to start by creating a simple while loop combined with in pairs that constantly searches for tools inside the player's character, then you want to use table.insert to add the tool into a table. Then finally using a for loop to print the contents of the table. KardashevScale 110 — 5y

1 answer

Log in to vote
0
Answered by
poke7667 142
5 years ago

Easiest way it to find out what tool equipped is to use the event called Activated. Assuming this is in a localscript and you want all tools to be checked:

local plr = game.Players.LocalPlayer
if #plr.Backpack:GetChildren() > 0 then -- Checks if there is any tools.
    for i, v in pairs(plr.Backpack:GetChildren()) do -- Iterate through the tools.
        if v:IsA("Tool") then -- Checks if the child is a tool.
            v.Activated:connect(function() -- Checks if it's equipped.
                -- code here
            end)
        end
    end
end

or if you want to find out just for one tool:

local plr = game.Players.LocalPlayer
if #plr.Backpack:GetChildren() > 0 then -- Checks if there is any tool.
    plr.Backpack["toolnamehere"].Activated:connect(function()
        -- code here
    end)
end
Ad

Answer this question