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 3 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:

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

Log in to vote
0
Answered by 3 years ago

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.

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 — 3y
Ad

Answer this question