Custom Health Script Doesn't Reset Character

I’ve made a Health script that uses 2 Values, when the player’s health reaches 0 or below, its supposed to add a 1 second delay player dies, but It doesn’t Kill the Player…

Video:

i tried putting Humanoid.Health = 0 and :BreakJoints(), both of em did not work, i don’t know Why its not killing the Player, if anyone knows any workarounds, i would like to hear it!

Try doing Humanoid:ChangeState(Enum.HumanoidStateType.Dead), which will force the humanoid into the dead state, thus killing it.

Also, if that doesn’t work, could you please share your code or other resources to further assist in debugging? Thanks.

alright, i tried that but it did not work…

here’s the whole code, i’m still abit new to scripting so expect it to be abit messy:

local HealthManager = script.Parent
local MaxHealth = HealthManager:WaitForChild("MaxHealth")
local character = HealthManager.Parent
local humanoid = character:WaitForChild("Humanoid")

-- setting values just in case
if MaxHealth.Value <= 0 then
	MaxHealth.Value = 100
end

if HealthManager.Value == nil then
	HealthManager.Value = MaxHealth.Value
end

humanoid.MaxHealth = MaxHealth.Value
humanoid.Health = math.clamp(HealthManager.Value, 0, MaxHealth.Value)

local isDead = false

-- alr we start with the functions
local function updateHealth()
	local currentHealth = HealthManager.Value

	if currentHealth <= 0 and not isDead then
		isDead = true
		task.delay(1, function() -- maybe i should use task.wait(1), idk..
			if humanoid and humanoid.Health > 0 then
				humanoid:ChangeState(Enum.HumanoidStateType.Dead) -- tried this, did not work
			end
		end)
	elseif currentHealth > 0 and isDead then
		isDead = false
	end

	if not isDead and humanoid.Health > 0 then
		humanoid.Health = math.clamp(currentHealth, 0, MaxHealth.Value)
	end
end

HealthManager:GetPropertyChangedSignal("Value"):Connect(updateHealth)

MaxHealth:GetPropertyChangedSignal("Value"):Connect(function()
	
	local oldMax = humanoid.MaxHealth
	local ratio = oldMax > 0 and HealthManager.Value / oldMax or 1

	humanoid.MaxHealth = MaxHealth.Value
	local newHealth = MaxHealth.Value * ratio
	HealthManager.Value = newHealth

	if not isDead then
		humanoid.Health = math.clamp(newHealth, 0, MaxHealth.Value)
	end
end)

updateHealth()

this is placed inside StarterCharacterScripts:

image

Hi again, I replaced line 28 with what I originally assume you had as Humanoid.Health = 0 and everything seems to be working.

Your code is a server-side script. Are you changing the health value on the server?

i am using 2 buttons that has a localscript to damage and heal, was i doing it wrong??

Localscripts run on the client-side of Roblox’s client-server runtime, whereas regular Scripts run on the server-side. Anything changed from a regular, server-side script replicates to all clients, meaning if a Script updates the health value, then every Player will “see” that change. In most circumstances except for some special exceptions, a Localscript that makes changes will not replicate to the server.

Because you updated the health value locally, your client “sees” the changed value, but the server doesn’t acknowledge this change and keeps the value the same. For example, if the health value was 100 and you set the value to 0 from a Localscript, a server-side Script would print the value as 100 still.

2 Likes

oohh, i get it now, thats why it wasn’t killing the player, thanks!