Unequip tool when health is low

Hi, I have this script that is supposed to unequip the tool when health is <= 5, however, it doesn’t seem to work? This is a script inside the tool.


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

The health check in your script runs only once, at the time of the tool being equipped. You need to also listen for when the health changes, so that it can check if it goes below the threshold whenever it changes.

local LOW_HEALTH_THRESHOLD = 5

tool.Equipped:Connect(function()
	local character = tool.Parent
	local humanoid = character:WaitForChild("Humanoid")
	
	local function unequipIfLowHealth(health)
		if health <= LOW_HEALTH_THRESHOLD then
			humanoid:UnequipTools()
		end
	end

    local healthChangedConnection = humanoid.HealthChanged:Connect(unequipIfLowHealth)
    tool.Unequipped:Once(function()
		healthChangedConnection:Disconnect()
    end)
    
	unequipIfLowHealth(humanoid.Health)
end)
2 Likes

basically ur only checking the humanoids health when u equip the tool.

put a local script in your tool and see if this works: :slight_smile:

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

local tool = script.Parent

    tool.Equipped:Connect(function()
        if humanoid.Health <= 5 then
            tool:Unequip() -- unequip the specific tool
        end
    end)

humanoid.HealthChanged:Connect(function(health)
    if health <= 5 then
        local equippedTool = character:FindFirstChild(TOOL_NAME)
        if equippedTool then
            equippedTool:Unequip()
        end
    end
end)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.