Force Unequip Code

How do I make players have their tools forced to be unequipped? What I mean is like this video: How To Make A Safe Zone (Roblox Scripting) - YouTube. Ive tried it, but it doesn’t work, so I want to ask the devforum.

1 Like
local tool = script.Parent

tool.Equipped:Connect(function()
	local character = tool.Parent
	local humanoid = character.Humanoid
	if humanoid then
		if humanoid.Health then
			if humanoid.Health <= 0 then
				humanoid:UnequipTools()
			end
		end
	end
end)

Script inside tool.

1 Like

wait how do I make it continuously work until I want it to stop

You can use a while loop, or remove the tools from their backpack, save them and then give them back when they exit the zone.

2 Likes

Well, you can attach an event to remove them whenever they are created, and then remove it once they leave the safe zone. (Note: the Backpack gets created and destroyed every time the player dies)

local player = game.Players.LocalPlayer

local removeToolsConnection
player.CharacterAdded:Connect(function(char)
    removeToolsConnection = player.Backpack.ChildAdded:Connect(function(child)
        child:Destroy()
    end)
end)

And once they leave you can simply do:

removeToolsConnection:Disconnect()
2 Likes

sorry I never stated what I was using this for, it’s not a safe zone, its a reviving script. I want the reviver and the downed person to both have their weapons unequipped looped while they are still reviving/being downed

Okay, you can apply the same code anyway.
Once the player starts reviving:
First, remove all tools:

for _, tool in pairs(player.Backpack:GetChildren()) do
    tool:Destroy()
end

Then call the first bit of code.

Once the player is revived, call the second bit above.

what does this do? it destroys my tools, but they are gone, so that’s kinda pointless

Whoops! I didn’t know you wanted to keep them afterwards. No worries, just a simple tweak.
Create a table to store all tools:

local unequippedTools

Add current tools

for _, tool in pairs(player.Backpack:GetChildren()) do
    table.insert(unequippedTools, tool)
    tool.Parent = nil
end

Remove any subsequent tools

local removeToolsConnection
player.CharacterAdded:Connect(function(char)
    removeToolsConnection = player.Backpack.ChildAdded:Connect(function(tool)
        table.insert(unequippedTools, tool)
        tool.Parent = nil
    end)
end)

Re-add tools:

removeToolsConnection:Disconnect()
for _, tool in pairs(unequippedTools) do
    tool.Parent = player.Backpack
end
unequippedTools = {}

what is the local player? (3.0 bypass)

You don’t have to use a while loop at all. You can just set the Tool | Roblox Creator Documentation property to false before unequipping it and setting it to true once you want them to be able to equip it again.

1 Like

how would I make it select all of the tools in their inventory and do that? i’m not the best at scripting

Well, like @Forummer pointed out, you can use the Humanoid | Roblox Creator Documentation method to unequip the tool, if any, that is currently equipped. Then, because all equiped tools should be in the player’s backpack, you can then loop through and diable them all.

In this instance, we just need to use the unequip method and a for loop to iterate through the players backpack. Here’s an example in a local script:

local player = game:GetService("Players").LocalPlayer
local backpack = player.Backpack

local humanoid = (player.Character or player.CharacterAdded:Wait()):WaitForChild("Humanoid")


local function EnableTools()
	for _, child in pairs(backpack:GetChildren()) do
		if child:IsA("Tool") then
			child.Enabled = true
		end
	end
end

local function DisableTools()
	humanoid:UnequipTools()
	
	for _, child in pairs(backpack:GetChildren()) do
		if child:IsA("Tool") then
			child.Enabled = false
		end
	end
end

I’m using a normal script, so what would I call for the player’s backpack? I think the local player = game:GetService("Players").LocalPlayer is only for local scripts

Oh. In that case you can use an edited version.

local function EnableTools(player)
	local backpack = player:FindFirstChild("Backpack")
	if backpack then
		for _, child in pairs(backpack:GetChildren()) do
			if child:IsA("Tool") then
				child.Enabled = true
			end
		end
	end
end

local function DisableTools(player)
	(player.Character or player.CharacterAdded:Wait()):WaitForChild("Humanoid"):UnequipTools()
	
	local backpack = player:FindFirstChild("Backpack")
	if backpack then
		for _, child in pairs(backpack:GetChildren()) do
			if child:IsA("Tool") then
				child.Enabled = false
			end
		end
	end
end

The player argument is the player you wish to disable tools for.

2 Likes

Setting the tool’s enabled value to false doesn’t do anything.

Strange. It should, according to the documentation.

If it isn’t, then I’d suggest using the StarterGui | Roblox Creator Documentation method to toggle whether the Backpack | Roblox Creator Documentation is enabled or not. You can do this like so:

local StarterGui = game:GetService("StarterGui")
--other code in-between
StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false) -- set to false to disable and true to enable

If you want, you can read more about it here: Disabling Default UI Elements | Roblox Creator Documentation

script.Parent.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	if player and humanoid then
		humanoid:UnequipTools()
	end
end)

Just paste this into a serverscript in the safezone part and it should work.
Hope this helped!

1 Like

You mean like this?

local StarterGui = game:GetService("StarterGui")

local function EnableTools(player)
StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, true) -- set to false to disable and true to enable
			end
		end
	end
end

local function DisableTools(player)
	StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false) -- set to false to disable and true to enable
			end
		end
	end
end
1 Like

You don’t have to make it that complicated. All you simply have to do is set the parent of the tool to the backpack of the player:

Tool.Parent = Backpack

1 Like