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

How to detect if a tool is equipped?

Asked by 6 years ago

For example, if I have a punching script and I do not want it to activate if a player has a tool equipped.

3 answers

Log in to vote
0
Answered by
hellmatic 1523 Moderation Voter
6 years ago

When you equip a tool, the tool's parent gets set to your character.

You can use a loop and check if there is a tool inside the player's character.

repeat wait() until game.Players.LocalPlayer.Character:IsDescendantOf(workspace)

local Character = game.Players.LocalPlayer.Character

function CHECK_TOOL(character)
    for _, v in pairs(character:GetChildren()) do 
        if v:IsA("Tool") then 
            print("tool found!")
            return true 
        end
    end
end

function PUNCH()
    if not CHECK_TOOL(Character) then 
        -- code
    end
end
0
Did not know that, cool thanks. TiredMelon 405 — 6y
0
No, no, no. sickings, NO! Don't write a loop that does nothing but `wait()` for something to happen! Use events instead! Link150 1355 — 6y
0
CharacterAdded and repeat wait() until Character:IsDescendantOf(workspace) do the same thing. I just prefer using repeat wait() until.. it works on my gun script lol hellmatic 1523 — 6y
0
No, they don't do the same thing. The result is functionally the same, but `repeat wait() until whatever` is very inefficient. Link150 1355 — 6y
0
You just said they don't do the same thing then say they are functionally the same.. hellmatic 1523 — 6y
Ad
Log in to vote
3
Answered by
Link150 1355 Badge of Merit Moderation Voter
6 years ago
local tool = character:FindFirstChildWhichIsA("BackpackItem")

if tool then
    -- the character has a tool equipped
else
    -- the character does not have a tool equipped
end

This is the proper way to check for an equipped tool. It will detect tools as well as hopperbins. This works because both the Tool and HopperBin classes inherit the "BackpackItem" abstract class.

0
I'm having an error that says "character is an nil value." I don't know why? But, I think your way works good.. Mrmonkeyman120 65 — 5y
0
umm sorry for the very late response but you have to first get the player and then the character for this script to work check on this site to find out how seikkatsu 110 — 5y
Log in to vote
-2
Answered by 6 years ago

Insert this script inside your handle.


tool = script.Parent.Parent tool.Equipped:connect(function() print("Tool Equipped") end)

Answer this question