Negative humanoid health fix

  1. I’m trying to force the server to kill any object that has a humanoid health value below 0 because for some reason the humanoid:takedamage function is causing the health to go into the negatives and won’t fire the is.dead function but I’m not getting any errors.

  2. The issue is stated as the above, the humanoid health will go passed 0 into the negatives and doesn’t seem to be firing the is.dead function, so I’m trying to find work arounds, here’s a screenshot of what’s happening. What’s interesting is that the humanoid will eventually reach 0 after a few seconds or so.
    image

  3. I’ve tried calling for all humanoids and their health value but for some reason it only works on players and not non player models with humanoids, I’ve tried forcing them to delete any non player humanoid models when their humanoid health goes to 0 but that still hasn’t worked, perhaps I’m coding it incorrectly?

All I’m trying to do is force the humanoid of every humanoid to reach instantly go to zero if the humanoid reaches below 0 for any interval.

-- HumanoidHealthFixer Script

-- Get the Players service
local Players = game:GetService("Players") and ("Models")

-- Function to fix humanoid health
local function fixHumanoidHealth(humanoid)
    if humanoid.Health < 0 then
        humanoid.Health = 0
    end
end

-- Connect to the PlayerAdded event
Players.PlayerAdded:Connect(function(player)
    -- Connect to the CharacterAdded event
    player.CharacterAdded:Connect(function(character)
        -- Check if the character has a Humanoid
        if character:FindFirstChild("Humanoid") then
            -- Fix the humanoid health
            fixHumanoidHealth(character.Humanoid)
        end
    end)
end)

-- Iterate through all existing players
for _, player in Players:GetPlayers() do
    -- Check if the player has a character
    if player.Character then
        -- Check if the character has a Humanoid
        if player.Character:FindFirstChild("Humanoid") then
            -- Fix the humanoid health
            fixHumanoidHealth(player.Character.Humanoid)
        end
    end
end

-- Iterate through all models in the workspace
for _, model in game.Workspace:GetDescendants() do
    -- Check if the model has a Humanoid
    if model:FindFirstChild("Humanoid") then
        -- Fix the humanoid health
        fixHumanoidHealth(model.Humanoid)
    end
end

Not exactly sure what I’m doing wrong here?

local Players = game:GetService("Players")

-- Function to fix humanoid health
local function fixHumanoidHealth(humanoid: Humanoid)
	humanoid:GetPropertyChangedSignal("Health"):Connect(function()
		if humanoid.Health < 0 then
			humanoid.Health = 0
		end
	end)
end

-- Connect to the PlayerAdded event
Players.PlayerAdded:Connect(function(player)
	-- Connect to the CharacterAdded event
	player.CharacterAdded:Connect(function(character)
		-- Check if the character has a Humanoid
		if character:FindFirstChild("Humanoid") then
			-- Fix the humanoid health
			fixHumanoidHealth(character.Humanoid)
		end
	end)
end)

this should fix it. but the health shouldnt even go below 0 in the first place. did you disable the death state in the humanoid?

Not that I’m aware of, I think it’s possibly the humanoid:takedamage function, I think the issue is only with models that possess humanoids such as NPCs, I haven’t been able to test the system on another player yet so I’m not entirely certain if the same bug happens to players, I’m focusing on players as well as other humanoid objects just in case it’s a both and issue.

I can check the script again to see if it’s somehow disabling humanoid death states.

So far I don’t see anything that is disabling the humanoid death states.

Are you running this in a Local or Server script?
I’m pretty sure NPC Humanoids are server based.

Also instead of searching all the models in the workspace why not put all the NPCs in a folder and just search through that, and write your code to search for the specific location of the Humanoid instead of doing game.Workspace:GetDescendants. Try game.foldername:GetDescendants. If you’ve got a lot of stuff in the workspace it’ll save some time.

Sounds good, I can definitely give it a try, I’ll let you guys know if I’m able to get it working.

Also, yes I’m running it as a serverscript.

It was only being registered client side on my weapon… smh

1 Like

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