How to make an IntValue value cannot rise more than a defined value

Well, basically I am making a system that by defined the maximum value of an intValue is 100, but then the value is lowered and an item is used to increase the value. My problem is that I want the sum limit to be 100 but I don’t know how to do it…

local tool = script.Parent
local Players = game:GetService("Players")

local del = 1

local function DestroyTool()
	wait(del)
	tool:Destroy()
end

tool.Activated:Connect(function()
	local char = script:FindFirstAncestorWhichIsA("Model")
	local Player = Players:GetPlayerFromCharacter(char)

	local DataPlayer = Player:WaitForChild("DataPlayer")
	local NeedsPlayerStats = DataPlayer:WaitForChild("NeedsPlayerStats")
	local StacksValue = DataPlayer:WaitForChild("ItemStacks")

	if StacksValue.WaterStack.Value <= 0 then
		DestroyTool()
	else
		StacksValue.WaterStack.Value -= 1
		NeedsPlayerStats.Thirst.Value += 10
		if StacksValue.WaterStack.Value <= 0 then
			DestroyTool()
		end
	end
	if NeedsPlayerStats.Hunger.Value >= 100 then
		NeedsPlayerStats.Hunger.Value = 100
	elseif NeedsPlayerStats.Thirst.Value >= 100 then
		NeedsPlayerStats.Thirst.Value = 100
	end 
end)

Could you not use math.clamp(val, min, max)?

how?, I really don’t know how to use the math.clamp

Simple, the first argument takes any number, the second argument indicates what’s the minimum number to be returned, the third is the maximum.

Example:
math.clamp(1,1,10)
Returns 1 because 1 is lower than or equal to the integer in second argument which is 1.

Example:
math.clamp(5,3,10)
Returns 5 because it’s between 3 and 10.

Example:
math.clamp(200,1,100)
Returns 100 because 200 is more than the integer in the third argument, which is 100, so it will return the maximum number.

Try adding print statements to keep track of what’s the script doing.

tool.Activated:Connect(function()
	...

	if NeedsPlayerStats.Hunger.Value >= 100 then
        print("Hunger over 100")
		NeedsPlayerStats.Hunger.Value = 100
        print("Attempted to lower hunger to 100")
	elseif NeedsPlayerStats.Thirst.Value >= 100 then
        print("Thirst over 100")
		NeedsPlayerStats.Thirst.Value = 100
        print("Attempted to lower thirst to 100")
	end 
end)

To make this a little easier, you can make a separate function for the limit:

NeedsPlayersStats.Hunger:GetPropertyChangedSignal("Value"):Connect(function(
   if NeedsPlayerStats.Hunger.Value >= 100 then
       NeedsPlayerStats.Hunger.Value = 100
   end
end)

-- For thirst

Put this in a script in the IntValue:

local maxValue = --Enter max value here

script.Parent.Changed:Connect(function()
if script.Parent.Value > maxValue then
script.Parent.Value = maxValue
end
end)
1 Like

You can just use .Changed since values only fire Changed when their Value property is changed.

2 Likes

If you are looking for another solution that doesn’t require any code, you can still use IntConstrainedValue | Roblox Creator Documentation which is deprecated yet still functional.