Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Removing a tool while equipping it?

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

✓ Accepted answer

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.

1 comment

0

Thank you! The script worked! And you also made me learn something new!

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

This discussion is preserved as it was. New answers, comments, and votes are closed.