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

Removing a tool while equipping it?

Asked by 8 years ago

I was trying to remove a sword even when the player equipped it. Here is the code I typed in:

for _,Player in pairs(game.Players:GetPlayers()) do
    local sword = Player.Backpack:FindFirstChild('ClassicSword')
    if sword then
        sword:Destroy()
    end
end

I tested it but didn't work. In Explorer, there was nothing in BackPack (which could be found in Player). But the sword was still in the player's hand. What should I do? (If this question was answered, pls send me a link to it)

2 answers

Log in to vote
0
Answered by
dyler3 1510 Moderation Voter
8 years ago

When they player equips a tool, it moves from their backpack into their character. This is actually quite a simply fix, all you have to do is check whether or not the tool is in the players backpack OR the players character.

Here's what that might look like:

for _,Player in pairs(game.Players:GetPlayers()) do
    if Player.Backpack:FindFirstChild('ClassicSword') then
        Player.Backpack:FindFirstChild('ClassicSword'):Destroy()
    elseif Player.Character:FindFirstChild('ClassicSword') then
        Player.Character:FindFirstChild('ClassicSword'):Destroy()
    end
end

Anyways, I hope this helped. If not, or if you have any further problems/questions, please leave a comment below, and I'll see what I can do.

0
Thank you! The script worked! And you also made me learn something new! starlebVerse 685 — 8y
Ad
Log in to vote
0
Answered by
Shawnyg 4330 Trusted Badge of Merit Snack Break Moderation Voter Community Moderator
8 years ago

Well, if it's in their hand, that means it's within their Character! You'll need to go through the Character, see what is a tool, and remove it!

for i,v in pairs(game.Players:GetPlayers()) do
    local sword = Player.Backpack:FindFirstChild('ClassicSword')
    if sword then
        sword:Destroy()
    else
        for _,b in pairs(v.Character:GetChildren()) do -- Go through the character
            if b.Name == "ClassicSword" then -- or if b:IsA("Tool")
                b:Destroy()
            end
        end
    end
end

Answer this question