How to change health regen amount?

I’m trying to change the health regen amount in my game to 3. I’ve got it figured out but my issue is that when the player’s health reaches 0, the game keeps adding +3 health to player, if that makes sense. My script is down here below if you want to check it and try it out in your game and see the error yourself. Also, I have it placed in the ‘StarterCharacterScripts’ folder:

local player = game.Players.LocalPlayer
local chr = player.Character
script.Parent = chr

local healthScript = chr:WaitForChild("Health")

healthScript:Destroy()

script.Name = "Health"

-- Gradually regenerates the Humanoid's Health over time.

local REGEN_RATE = 3/100 -- Regenerate this fraction of MaxHealth per second.
local REGEN_STEP = 1 -- Wait this long between each regeneration step.

--------------------------------------------------------------------------------

local Character = script.Parent
local Humanoid = Character:WaitForChild'Humanoid'

--------------------------------------------------------------------------------

while Humanoid.Health > 0 do
	while Humanoid.Health < Humanoid.MaxHealth do
		local dt = wait(REGEN_STEP)
		local dh = dt*REGEN_RATE*Humanoid.MaxHealth
		Humanoid.Health = math.min(Humanoid.Health + dh, Humanoid.MaxHealth)
	end
	Humanoid.HealthChanged:Wait()
end
1 Like

You can’t add health in local scripts because of roblox Update (FE update).

1 Like

Yes, I’m aware of that. I tryed making the script a ‘Script’, but it gave me an error on line 2.

You can’t call line one in Script.
local player = game.Players.LocalPlayer

So I remove it? If so, how do I then put the script into the player character model?

You can use remote event and function Custom Events and Callbacks | Documentation - Roblox Creator Hub. But just to know exploiters can call it from client and give them self health.

I don’t really understand remote functions and events.

Just grab the health script from your character copy and paste, put into starterCharacterScripts, and change the wait to what ever you want and the fraction to change the amount it changes when it does go up.

10 Likes

You can put it in starterCharacterScripts and put that line

if script.Parent:FindFirstChild("Humanoid") then
local character = script.Parent
local humanoid = script.Parent.Humanoid
    end

That isn’t even needed, the reason he is getting the 3+ hp is because he has 3/100, just change that to 1/100

So all I do is copy the script, put it into ‘StarterCharacterScripts’ and change the regen amount?

1 Like

Yes, and the wait time if you want(It seems weird putting a server script in starterCharacterScripts but it works

1 Like

I’m just fixing his first mistake adding health from local script.

But, wouldn’t I need to remove the script from the character?

No, the script gets override, the point is you don’t want to destroy health, hope I’ve helped

1 Like

Ok, I’ll copy what’s inside the health script, make a new script in startercharacterscripts, and then just paste the code into it.

Make sure the script is named “Health”, so it can override the health script that’s in the character

1 Like

Ok, I’ve changed the name and it works perfectly.Thank you both so much for the assistance.

1 Like

There is a script called Health inside the player’s character.
You can remove it and modify this (server-script):

local REGEN_RATE = 1/100 -- Regenerate this fraction of MaxHealth per second.
local REGEN_STEP = 1 -- Wait this long between each regeneration step.

local Character = script.Parent
local Humanoid = Character:WaitForChild'Humanoid'

while true do
	while Humanoid.Health < Humanoid.MaxHealth do
		local dt = wait(REGEN_STEP)
		local dh = dt*REGEN_RATE*Humanoid.MaxHealth
		Humanoid.Health = math.min(Humanoid.Health + dh, Humanoid.MaxHealth)
	end
	Humanoid.HealthChanged:Wait()
end
local Players = game:GetService('Players')
local NewHealth = script.Health

Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		local Health = Character:FindFirstChild('Health')
		if Health then
			Health:Destroy()
			NewHealth:Clone().Parent = Character
		end
	end)
end)