Im making a level game, and i want to make so it will switch levels. All of the levels are client, and to switch levels i need to fire an event from client. The problem is that any roblox exploiter can fire this event and access levels. Can you prevent that in any possible way? If theres not, recommend me some ways for the game to work.
No. Just validate input. Don’t trust the client, as the client has FULL control over themselves. Why can’t you store these levels on the server, then only send them to the client if they are allowed to have it?
Just check on the server side if the player has permission to change to that specific level or not. On the server side you should only rely on the first player argument remotes pass automatically to validate which player is firing it, since it can’t be spoofed.
I would also recommend making the permission check on both the client and server, so that you know they exploited if they bypass it.
Example client script:
--A shared module for checking if a user has permission to perform an action
local permissions = require(game.ReplicatedStorage.Permissions)
local targetLevel = 5
--Client check to protect normal players from server check
if permissions.canChangeLevelTo(game.Players.LocalPlayer, targetLevel) then
levelSwitchRemote:FireServer(targetLevel)
end
Server script:
local permissions = require(game.ReplicatedStorage.Permissions)
levelSwitchRemote.OnServerEvent:Connect(function(player, targetLevel)
if not permissions.canChangeLevelTo(player, targetLevel) then
player:Kick("You exploited to change the level")
return
end
--here you actually change the level instead of just printing
print("Changing level to "..targetLevel)
end)
canChangeLevelTo is a function you setup that returns a boolean(false or true) based on if the player has permission or not, for example you can validate this by making a datastore request or checking in-game loaded data. You can use RunService to check if the module is running on the client or server, so you validate through a different method(for example since we can’t check datastores on the client you can make a remote the module uses to make a server call to that function).
In a real case scenario you should perform more checks except of just this, including input validation(ensuring targetLevel is a number, an integer, and actually corresponds to a valid level) and rate limiting the remote(basically if the player fires the remote very fast, kick them to avoid overwhelming the server, to avoid kicking normal players add the rate limit on client as well).
Lastly I would also recommend that you store the levels inside ServerStorage instead of the client. Not only to ensure that exploiters don’t have access to them, but to also reduce any lag or memory issues on the client side.