Hello users again, well, I have a new question, I want to set a limit on my datastore to the maximum of 16000 however it seems that I am not doing right.
game.Players.PlayerAdded:Connect(function(player)
local Folder = Instance.new("Folder")
Folder.Parent = player
Folder.Name = "leaderstats"
local Cash = Instance.new("IntValue")
Cash.Parent = player.leaderstats
Cash.Name = "Cash"
if Cash.Value == 16001 then
Cash.Value = 15999
end
end)
What should I do to set a money limit so that if any user exceeds 16000 or above?
You are checking if the value of the cash is over 16000 only when you initialize it. You need to check if the value of the cash is over 16000 every time the value of the cash changes.
Cash.Changed:Connect(function()
local value = Cash.Value
if value > 16000 then
Cash.Value = 16000;
end
end)
The Cash.Value will be set to newValue but if it is lower than lowerBound it will be set to lowerBound. If it is higher than upperBound it will be set to upperBound.
math.clamp is functionally equivalent to:
local function clamp(x, lowerBound, upperBound)
if x > upperBound then
x = upperBound
elseif x < lowerBound then
x = lowerBound
end
return x
end
You will want to replace x variable with the variable containing new value you are setting the Cash.Value to.
Or perhaps you can easily do something like this: Cash.Value = math.clamp(Cash.Value, 0, 16000)
This will set the Cash's Value property to be its self but clamped to a range of [0, 16000]. This means if their cash is lower than 0 it will become 0 and if it is higher than 16000 it will become 16000.