Why does the health regeneration not change when the eatamount is less then 75?

Why is it not changing the health regen amount whenever the eat amount is less then 75? I tried this with atleast 20+ different ways and all of them didnt work. The script here is in StarterCharacterScripts, and called “Health”. Every way i tried also didn’t leave anything in the output at all.

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

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")

local EatProperties = Character:WaitForChild("EatProperties")

local EatAmount = EatProperties:WaitForChild("EatAmount")

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

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

if EatAmount.Value <= 75 then
REGEN_RATE = 0/100
else
REGEN_RATE = 1/100
end

By the way eat properties and eat amount are always in the character, every time i checked, because i made them spawn in the character.

Is the EatAmount constantly changing? If so, you need to constantly check the value using a while loop.

I used a loop before too, it didnt work, and the way currently how i change the eat amount (because i havent made a system for it yet), is by just going into the character and changing the value.

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

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")

local EatProperties = Character:WaitForChild("EatProperties")

local EatAmount = EatProperties:WaitForChild("EatAmount")

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

spawn(function()
	while true do
		if Humanoid.Health < Humanoid.MaxHealth then
			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
end)

spawn(function()
	while true do
		if EatAmount.Value <= 75 then
			REGEN_RATE = 0/100
		else
			REGEN_RATE = 1/100
		end
	end
end)

You had an unnecessary nested while loop in the first block after the dashed lines which I’ve swapped for an if statement instead, I’ve also converted the second block of code into a while loop as well and placed both blocks within calls to the built-in function named spawn so that both while loops will run at the same time.

1 Like

Thanks, i was wondering if maybe my loops werent correctly done.