Questions about setting amount of money in a player

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?

Everytime you give or remove “cash” from a player you can check how much they have and make sure it’s not over the limit.

 if cash.Value >= 1600 then
    cash.Value = 1600
 end
2 Likes

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)
2 Likes

You can also use an IntConstrainedValue to make sure their money can never go above the limit.

Edit: this object is deprecated. I would recommend using math.clamp, as shown below

1 Like

Does not work, it requires studio api turned on?

It doesn’t work either, but maybe because I haven’t tested it on servers yet

I would just like to note that IntConstrainedValue is deprecated.

I suggest you use the math.clamp function for this.

Cash.Value = math.clamp(newValue, lowerBound, upperBound)

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

This object has been deprecated and I also used it for these situations.

Dang, wouldn’t have posted had I known. Mobile developer hub does not display deprecated messages :frowning:

1 Like

ok, I’ll see if it works, thanks

I would make a feature request about that.

2 Likes

If you are only checking once the player joins then it just checks once. You need to check whenever you change a player’s cash value.

Does this example fit? local function clamp(x, 0, 16000)? I think I’m doing something male, x is indicated by null

1 Like

“x is indicated by null” = is giving a null error with x

It seems that the file is not completely discontinued, only it can only be generated by instances

Hello. Sorry I am 3 hours late, I was busy.

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.

2 Likes