local player = game.Players.LocalPlayer
local value = player.Statistics.CoinValue.Value
while true do
if value > 100 then
value = 100
end
end
i have a data store that saves coins and i wanted to cap it to a certain number so i set an infinite loop checking if the value is more than 100 and if it is then set it back to 100
This is a very horrible way to set a limit with your coins, I recommend using math.min or math.clamp to manage limits of Numbers.
Plus, you are getting a Value, Not the Instance itself
Coins.Value = math.min(100, Coins.Value)
-- gets and returns the lowest number
-- or:
Coins.Value = math.clamp(Coins.Value, 0, 100)
-- 0 is the minimum number
-- 100 is the max number
Yes, But here is the thing, there is a Difference between Getting the Instance, than Getting the Value
Since you are getting the Value (in this case a number), it will give you a number instead of an Instance, which is why you do this:
local Coins = Example -- this gives you an Instance
print(Coins.Value) -- A Number
-- not this:
local Coins = Example.Value -- this only gives you a number
print(Coins) -- A Number
Its probably best that you dont use this example, not because i said it was a rule Violation, but because it is used on the Client and Not the Server, and there is a Big Difference and a Good Reason for that
If you change something on the Client, it will not Replicate to the Server. This is a bad Thing because if you have something like a DataStore, You are Changing Values for a Instance, It would’t save the Data or Change the Value because as far as the Server is concerned, nothing happened, so its recommended you use the Server rather than the Client
However, if you are creating a UI and are trying to Replicate the amount of coins you have, it would work, otherwise, don’t do this.
Additionally, You can Adapt this Code to make it work on the Server.
No, I mean the Server, not RemoteEvents, The Server cant be Easily Manipulated unlike the Client, RemoteEvent are easy to access, and can be easily fire by exploiters, who can change their Amount to anything they want because it is done on the Client (Because of the RemoteEvent), Use the Server to Handle these kinds of things, Not the Client, Don’t even use a RemoteEvent for this as its useless and Risky to do so…