Equipping only one tool at once

So I’ve had and been having a problem trying to make a script that only makes the character able to equip one tool at once as there is a glitch with one of the tools which I cannot fix. No don’t ask me to fix that glitch because fixing this would be easier.

So I made this code using a server script, located in ServerScriptService and I’m getting no response in the output at all.

game.Players.PlayerAdded:Connect(function(plr)
	local chr = game.Workspace:WaitForChild(plr.Name)
	
	if plr then
		
		while true do
		wait(.001)
		local checked = 0
		local get = chr:GetChildren()
		
		for i = 1, #get do
			local getc = get[i]
			
			if getc:IsA('Tool') then
				checked = checked + 1
			end
		end
		
		if checked > 1 then
			chr.Humanoid:UnequipTools() 
			end
		end
	end
end)

It’s intended purpose is to unequip all tools from the character if more than 1 is equipped. Thank you for your time and I would love help on this :>

3 Likes

It isn’t exactly necessary to encase your checking thru a loop, you could just use a ChildAdded event on the Character instead & check if it’s a tool or not

chr.ChildAdded:Connect(function(NewChild)
    local Checked = 0
    local Objects = chr:GetChildren()

    for i = 1, #Objects do
        local Check = Objects[i]

        if Check:IsA("Tool") then
            Checked += 1
        end
    end

    if Checked > 1 then
        chr.Humanoid:UnequipTools()
    end
end)

Also you could just use a CharacterAdded event as well?

1 Like
game.Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function(chr)
local tools = 0
local humanoid = chr:WaitForChild('Humanoid')

chr.ChildAdded:Connect(function(child)
if child:IsA('Tool') then
tools += 1

if tools > 1 then
humanoid:UnequipTools()
end
end
end)

chr.ChildRemoved:Connect(function(child)
if child:IsA('Tool') then
tools -= 1
end
end)
end)
end)

I still can’t understand how you can equip more than 1 roblox tool in your game. . .

I’m using SolarHorizon’s lightsabers, you can do a thing called stacking them which is super easy and if you have more than one lightsaber it make you insanely OP.

image

Didnt work unforuntunately.

Maybe try using Jack’s solution.

It didn’t work either unfortunately…

local tools = 0
char.ChildAdded:Connect(function()
	curTools = 0
	for i,v in pairs(char:GetChildren()) do
		if v:IsA("Tool") then
			curTools += 1
		end
	end
	tools = curTools
	if tools > 1 then
		tools = 0
		char.Humanoid:UnequipTools()
	end
end)

I only just included a snippet of code as an example

game.Players.PlayerAdded:Connect(function(Player)
    Player.CharacterAdded:Connect(function(Character)
        local Humanoid = Character:WaitForChild("Humanoid")
        Character.ChildAdded:Connect(function()
            local Checked = 0
            local Objects = Character:GetChildren()

            for i = 1, #Objects do
                local Check = Objects[i]

                if Check:IsA("Tool") then
                    Checked += 1
                end
            end

            if Checked > 1 then
                chr.Humanoid:UnequipTools()
            end
        end)

    end)
end)
1 Like

Didn’t work again…

Oh, use Jacks version, he did CharacterAdded function

1 Like

I feel an alternative would be necessary at this point.

Jack made same thing as me but he added it so it works for you i think

IT WORKED LETS GOOOO!!! Thank you for your time :slight_smile:

1 Like

You could just mark my post as a solution if it worked for you in case anyone else struggles with this issue as well :slightly_smiling_face: @R0mAAn1CH was only stating that I added an additional CharacterAdded event & I fixed a couple of other things as well

Done, thanks for your efforts with the code!

2 Likes