Coin cap wont work

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

1 Like

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

Just one question, what do you mean “Plus, you are getting a Value, Not the Instance”

I have the value named to be CoinValue

You should use a function instead.

Fixed code:

--//Services
local Players = game:GetService("Players")

--//Variables
local LocalPlayer = Players.LocalPlayer
local CoinValue = LocalPlayer.Statistics.CoinValue

--//Functions
CoinValue.Changed:Connect(function()
	CoinValue.Value = math.min(CoinValue.Value, 100)
end)

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

I wouldn’t recommend just giving out code like this, its a bit of a rule Violation.

this script looks like it wont do anything.

Would I apply this to my existing script?

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.

Do you mean with remote events?

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…

I see now why my code didn’t work thanks!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.