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

How to check if tool is in Character/Equipped?

Asked by 4 years ago

I want to keep checking if my tool, pistol, is in the character. Here's the script I tried to make:

game.Players.PlayerAdded:Connect(function(player)
    local character = player.Character or player.CharacterAdded:Wait()
    while true do
        wait(1)
        for i, v in pairs(character:GetChildren()) do
            if v.Name == "Pistol" then
                print("Pistol found")
            end
        end
    end
end)
0
do you want to check if it's equipped? HeroFigo 81 — 4y
0
yes bittersweetfate 142 — 4y

1 answer

Log in to vote
1
Answered by
Ortron 27
4 years ago

Tool has two useful events which may be helpful for you, these are Equipped and Unequipped

Using your example you could detect what tool is equipped through this method:

game.Players.PlayerAdded:Connect(function(Player)
    Player.CharacterAdded:Connect(function(Character)
        for _, Tool in pairs(Player.Backpack:GetChildren()) do
            if Tool:IsA('Tool') then
                Tool.Equipped:Connect(function()
                    print(Tool.Name .. ' has been equipped by ' .. Player.Name)
                end)
            end
        end
    end)
end)
1
Yes. This is the way I would do it too. Using :FindFirstChild() is not needed here. (Related to the other answer too) appxritixn 2235 — 4y
Ad

Answer this question