Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Is there a way to put a max limit a IntValue?

I am trying to make a Hunger Thirst System and i cannot figure out how to limit the value on 100. so that a player cannot be 115 full of water.

This is the code that i have:

local Player = game.Players.LocalPlayer
local leaderboard = Player:WaitForChild("Leaderboard")
local Tool = script.Parent

Tool.Equipped:Connect(function(mouse)
	mouse.Button1Down:Connect(function()
		leaderboard.Thirst.Value = leaderboard.Thirst.value + 15
		Tool:Destroy()
	end)
end)

1 answer

✓ Accepted answer

You need to check if the value is smaller then 100 before adding this thirst value.

local Player = game.Players.LocalPlayer
local leaderboard = Player:WaitForChild("Leaderboard")
local Tool = script.Parent

Tool.Equipped:Connect(function(mouse)
	mouse.Button1Down:Connect(function()
		if leaderboard.Thirst.Value < 100 then -- Checking if the value is smaller then 100
			leaderboard.Thirst.Value = leaderboard.Thirst.value + 15
			Tool:Destroy()
		end
	end)
end)

This code should work fine.

1 comment

0

math.clamp could also work. It takes 3 parameters, the number to clamp, the minimum, the maximum. If the number is greater than the maximum it'll return the maximum. If the number is lower than the minimum it'll return the minimum. Otherwise, it'll return the same number.

This discussion is preserved as it was. New answers, comments, and votes are closed.