Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Preventing Health Regeneration? [Solved]

I'm working on limiting players' health in my game to prevent it from regenerating over time by default. Is there an easy method or property I can change to do this, or will I have to write some crazy script that records the current health and manually keeps it at that level?

1 answer

✓ Accepted answer

There is a script in every player named 'Health' that manages regeneration, shown below:

--Responsible for regening a player's humanoid's health

-- declarations
local Figure = script.Parent
local Head = Figure:WaitForChild("Head")
local Humanoid = Figure:WaitForChild("Humanoid")
local regening = false

-- regeneration
function regenHealth()
	if regening then return end
	regening = true
	
	while Humanoid.Health < Humanoid.MaxHealth do
		local s = wait(1)
		local health = Humanoid.Health
		if health > 0 and health < Humanoid.MaxHealth then
			local newHealthDelta = 0.01 * s * Humanoid.MaxHealth
			health = health + newHealthDelta
			Humanoid.Health = math.min(health,Humanoid.MaxHealth)
		end
	end
	
	if Humanoid.Health > Humanoid.MaxHealth then
		Humanoid.Health = Humanoid.MaxHealth
	end
	
	regening = false
end

Humanoid.HealthChanged:connect(regenHealth)

All you need to do is delete this script when a player respawns or spawns! To do this, insert this into a LocalScript in StarterGui:

game.Players.LocalPlayer.Character.Health:Destroy()

No crazy scripting involved! Hope I've helped! If you need any help, please comment, I'd be glad to fix your script or answer any questions.

2 comments

0

Ahh, ok thanks, and messing with that doesn't have any other strings attached to worry about?

0

None at all, but you should know that it's better practice to use Debris:AddItem() to destroy members of spawning Characters, as they have a tendency to not actually get destroyed otherwise.

This discussion is preserved as it was. New answers, comments, and votes are closed.