I am trying to make a level system but I want the max level to be 100. I am doing this by having a local script check for if the players level is higher then 100, and if it is then it fires a remote event that then checks again to make sure its higher than 100, but it seems to not be working.
Local Script:
if Level.Value > 100 then
wait()
LevelLimitEvent:FireServer()
end
Script:
local LevelLimitEvent = game.ReplicatedStorage.LevelEvents:WaitForChild("LevelLimit")
LevelLimitEvent.OnServerEvent:Connect(function(plr)
if plr.leaderstats.Level.Value > 100 then
plr.leaderstats.Level.Value = 100
end
end)
What you can simply do every time the player levels up is by clamping the next level; here’s what I mean by that:
local Level = Player.leaderstats.Level.Value
local MIN_LEVEL = 1
local MAX_LEVEL = 100
-- ...
-- When adding a level to the player (on the server)
Level.Value = math.clamp(Level.Value + 1, MIN_LEVEL, MAX_LEVEL)
(I may be wrong on this part)
It is important that all the Levels and checking for those should be done only on the server. I know you add a check to make sure the Player’s real level is >100, but an exploiter can manipulate what that if statement will retrieve. You should keep the stats manipulation and checking on the server. The LeveLimitEvent is useless at this point.
Strikes me as an XY Problem. Your X problem is wanting to clamp a level value and your Y solution is having the client use a remote to signal that the level overexceeds a value, but you’re having a problem with solution Y and are asking for suggestions on fixing that instead of problem X.
Get rid of the client-side work entirely. Push the work fully to the server.
When you’re working with data, especially if it’s crucial to game play, the client should never be authoritative of the decisions made in regards to its modifications. Have the server clamp the value - same code as what Nil_ScripterAlt posted.
Remember that in your project’s environment, RemoteEvents should be used only if you need to facilitate communication between environments. If there’s no reason for the client to request something or if there’s a proper/better method, you should take that.
Not necessarily, it depends on the way in which you’re using the event. In this case though, yeah it’d be appropriate. ValueObjects have a special implementation of the Changed RBXScriptSignal which fires as in when their value changes (or so I remember). This would actually lead to a direct solution to the problem, granted the work is being done entirely on the server.
local MIN_LEVEL = 1
local MAX_LEVEL = 100
... -- let's just say a variable called "Level" that references plr.leaderstats.Level exists somewhere here
Level.Changed:Connect(function (NewValue)
Level.Value = math.clamp(NewValue, MIN_LEVEL, MAX_LEVEL)
end)