Health doesn't return to normal after releasing key

I have a code that lets the player have infinite health when holding down F, it works when you hold the key down but the health still stays infinite when you release the key.

local keyPressed = false
local OldHealth = hum.Health

uis.InputBegan:Connect(function(key)

if key.KeyCode == Enum.KeyCode.F then
keyPressed = true
anim:Play()
hum.Health = math.huge
end
end)

uis.InputEnded:Connect(function(key)

if key.KeyCode == Enum.KeyCode.F then
keyPressed = false
anim:Stop()
hum.Health = OldHealth
end
end)

When increasing humanoid health, increase humanoid maxHealth too.

Everything looks fine. Have you tried setting “OldHealth” after the input has started? Make sure it’s before you set the health to math.huge. Oh, by the way, setting Health of a Humanoid above its max health will default it to it’s max health. So if your Humanoid’s max health was 100, setting Health to math.huge would make the Health 100 and not infinite. I’m assuming you didn’t know this already, and did it for you. Also, this is on the client. That means that if you were to set Health to something else on the server (or another client, and you should never modify damage on a client) it would apply to the client. So on your client it’d say inf, but the moment that you take damage on the server it will reset to what the server thinks it is. Add a RemoteEvent and implement sanity checks to prevent this.

EDIT: Going even further, controlling animations on the client will result in it only playing for the player who activated the animation, and not anybody else on the server. It will only replicate for the player who pressed the key and nobody else. The reason Roblox did this is for FilteringEnabled. Read more here (it has since been completely deprecated and what I said will always happen)

local keyPressed = false
local OldMaxHealth = hum.MaxHealth
local OldHealth = hum.Health

uis.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.F then
		keyPressed = true
		anim:Play()
		OldHealth = hum.Health
		OldMaxHealth = hum.MaxHealth
		hum.MaxHealth = math.huge
		hum.Health = math.huge
	end
end)

uis.InputEnded:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.F then
		keyPressed = false
		anim:Stop()
		hum.MaxHealth = OldMaxHealth
		hum.Health = OldHealth
	end
end)
2 Likes

Looks promising, ill test out what youve said tomorrow. Thanks for your guys response

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