Dividing number by time

So I am trying to make a system that you gain health over time, I have a stat called healthGoal
As the player grows I want them to gain health and when they’re at 100% growth they’ll be at healthGoal
All I need is a way to divide healthGoal by the growth rate which is Stats.Growthrate

1 Like

Is the HealthGoal similar to Humanoid.MaxHealth?

No, I will have it changing the MaxHealth over time
Or… yes? I don’t exactly know what you mean by that

I was just trying to think what each variable you mentioned represented. Could you explain the process a little bit more in detail? I can think of various ways to accomplish a similar task but not sure which one you are looking for.

So, the player will grow at a certain amount of time based off the creature they are (Stats.GrowthWaitTime)
I want them to gain more health as they grow but I want it to reach the goal health at the same time they are 100% grown

Just do this,

local Time = 0

while Health < HealthGaol do --Health is the players health.  This would be a variable
     Time += 1
     Health += (1 * Time)
     wait(1)
end

Going to assume this is being applied to the Humanoid’s Health property. If not, feel free to make the necessary changes, but here is the general idea:

Standard health regen:

local RegenRate = 2 -- seconds
local RegenAmount = 10 -- HP gained per RegenRate
local HealthGoal = Humanoid.MaxHealth

repeat
	task.wait(RegenRate)
	Humanoid.Health += RegenAmount
until Humanoid.Health >= HealthGoal

But that would regen until MaxHealth is reached which could take any number of seconds depending on how much health was missing.

If you want to always heal over a given specific time, no matter how much health is missing, you would need to find out how much health you need to give, and then divide that by how many seconds you want it to heal for. Like so:

local HealthNeeded = Humanoid.MaxHealth - Humanoid.Health
local RegenTime = 30 -- seconds
local RegenAmount = HealthNeeded / RegenTime
local HealthGoal = Humanoid.MaxHealth

repeat
	task.wait(1)
	Humanoid.Health += RegenAmount
until Humanoid.Health >= HealthGoal -- which should take 30 seconds as RegenTime specifies.

I am not trying to heal the player, I want them to gain a higher MaxHealth over time
Gaining it until they’re 100% grown while also not passing the goal

oh. Then wouldn’t it be easier to just add to their MaxHealth in proportion to what % of their CurrentGrowth to their MaxGrowth based on a MaxHealthGoal?

local BaseMaxHealth = 100 -- base at zero growth
local MaxHealthGoal = 200 -- max health at 100% growth
local GrowthPercent = CurrentGrowth/MaxGrowth
Humanoid.MaxHealth = math.clamp(MaxHealthGoal * GrowthPercent, BaseMaxHealth, MaxHealthGoal)

actually it might, I did not think of this

1 Like

I have included an example in my previous post. Let me know if this is the solution you were looking for, if not I will continue to try and help you out.

It did not work, it was adding by a ton

Here is a function

local function map(x, range1start, range1end, range2start, range2end)
	return (x-range1start)/(range1end-range1start) * (range2end-range2start) + range2start
end

It maps the value X from one range, to another.

So for example, say your creature is leveling up. Starts at level 0 and goes to 100 for the sake of example. Now you want your health to go up with it. Starting from 100 base health to increase to 500 base health over time. You can use the function as follows

The variables are put in in this order.
The level they are
Min level
Max level
MinHealth
MaxHealth

That returns how much health they should have based on a linear health gain from their level.

Or to show the example above if the player was level 10, starting from level 0 to level 100, health starts at 100 and goes to 500.

Humanoid.MaxHealth = map(10, 0, 100, 100, 500) --this will return 140
Humanoid.MaxHealth = map(50, 0, 100, 100, 500) -- this will return 300