Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 4 years ago

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:

01local Player = game.Players.LocalPlayer
02local leaderboard = Player:WaitForChild("Leaderboard")
03local Tool = script.Parent
04 
05Tool.Equipped:Connect(function(mouse)
06    mouse.Button1Down:Connect(function()
07        leaderboard.Thirst.Value = leaderboard.Thirst.value + 15
08        Tool:Destroy()
09    end)
10end)

1 answer

Log in to vote
0
Answered by 4 years ago

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

01local Player = game.Players.LocalPlayer
02local leaderboard = Player:WaitForChild("Leaderboard")
03local Tool = script.Parent
04 
05Tool.Equipped:Connect(function(mouse)
06    mouse.Button1Down:Connect(function()
07        if leaderboard.Thirst.Value < 100 then -- Checking if the value is smaller then 100
08            leaderboard.Thirst.Value = leaderboard.Thirst.value + 15
09            Tool:Destroy()
10        end
11    end)
12end)

This code should work fine.

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. killerbrenden 1537 — 4y
Ad

Answer this question