Limit Value Problem

Im trying to make it so the SodaValue doesn’t increase more than 100.
How could i do this?


--

local DrinkEvent = RepStorage.DrinkEvent

function IncreaseAmount(Character)
	local PlayerValues = Character:WaitForChild('PlayerValues')
	local SodaValue = PlayerValues:WaitForChild('SodaValue')
	
	if SodaValue.Value >= 100 then
		SodaValue.Value = 100
		print('soda value is 100 or more than 100')
		print('set soda value to 100')
		
		return
	end
	
	SodaValue.Value += 5
end

DrinkEvent.OnServerEvent:Connect(function(Player:Player)
	local Character = Player.Character or Player.CharacterAdded:Wait()
	IncreaseAmount(Character)
end)

You can use math.min

SodaValue.Value = math.min(SodaValue.Value+5, 100)

Also how could i decrease it instead

function DecreaseSodaValue(Character)
	local PlayerValues = Character:WaitForChild('PlayerValues')
	local SodaValue = PlayerValues:WaitForChild('SodaValue')
	
	spawn(function()
		while task.wait(SodaValueCD) do
			SodaValue.Value = math.min(SodaValue.Value - 1, 0)
		end
	end)
end

with math.max

SodaValue.Value = math.max(SodaValue.Value - 1, 0)

You could use a DoubleConstrainedValue or math.clamp(value, min, max)
To get the DoubleConstrainedValue, Go to studio settings and enable show deprecated objects
DoubleConstrainedValue is still widely used across games.

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