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
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.
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.