How can I make a value not go past 100 if the value regenerates

Haha yes you were probably wondering what I meant by the title of this topic as I did not do a good job of summarising it in under 10 words.

Basically Im making a system where if you drink/eat something it regenerates stamina/health.

Yeah sounds simple that’s what I thought too until I realised I don’t know how to prevent the stamina and health incrementing past 100.

So far I’ve tried to make a function in the main module script for all the items where the origin of this issue is.

AbilityAmount = The amount in which the health/stamina regenerates upon use
ChangedValue = the health/stamina value which will be incrementated

Module Script:

local Items = {
	["BloxyCola"] = {
		["Title"] = "Bloxy Cola",
		["Description"] = "An absolute classic! The Bloxy Cola is a tasty drink you can sip in the hot weather, regenerates stamina by a great amount.",
		["Rarity"] = "Common",
		["ImageId"] = "rbxassetid://578766873",
		["Useable"] = true,
		["Space"] = 1,
		["AbilityType"] = "Stamina",
		["AbilityAmount"] = math.random(10,15)
	}
}

function Items.Ability(name, ChangedValue)
	if Items[name] then
		if ChangedValue <= Items[name].AbilityAmount-100 then
			ChangedValue = ChangedValue + Items[name].AbilityAmount
		end
	end
end

return Items

Just check if the value is higher than 100, if it’s, change it to 100

(adapt to your script)

if healthOrStamina > 100 then
    healthOrStamina = 100
end

What if its something like 95 then the AbilityAmount would be added and it would go past 100 or Im just not getting something here.

yea, but the script I gave, will check if its 101 or higher, and if its, it changes to 100, so it can’t be higher

You could also do math.clamp(value, 0, 100)

4 Likes

Sorry could you explain what math.clamp does/means and how it works?

math.clamp is just a function where you input the number, x, that you are clamping, and the second number is the lowest that it can be, and the third number is the highest that it can be. If x is less than the lowest, then it will return whatever the low/second number is. if it is higher than the highest/third number, then it will return the third number. If x is in between both then it will just return x.

So how could I implement this in my code?

function Items.Ability(name, ChangedValue)
	if Items[name] then
		if ChangedValue > 100 then
			ChangedValue = 100
		else
			ChangedValue += Items[name].AbilityAmount
		end
		print(ChangedValue)
	end
end

ChangedValue = math.clamp(ChangedValue + Items[name].AbilityAmount, 0, 100)
Could also use math.min here if you only need to limit the number to an upper boundary,
ChangedValue = math.min(ChangedValue + Items[name].AbilityAmount, 100)
math.min returns the smallest value among all of the arguments passed to it.

So in the case where ChangedValue + amount < 100, ChangedValue + amount is the smallest among those numbers so that’s what would be returned. In the case where ChangedValue + amount > 100, 100 would be the smallest among the numbers passed to math.min.

1 Like

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