When should I use Local Scripts, Server Scripts, And Module Scripts?

I want to optimize my game, and learn when Is the best times to use these scripts, to prevent exploiters from causing problems, and improve my game’s in general.

When should I use Local Scripts, Server Scripts, And Module Scripts?

2 Likes

Well.

When do you need something to happen clientside versus server side?

Modulescripts, use them for functions that you use more than one place. For example in a block mining game, you will check if the player is close enough to the block both on the client and on the server, so a function like that would be put into a ModuleScript.

You can also use ModuleScripts to store data. If you for example have a combat game with melee weapons, you can store the weapon length, weapon damage, weapon cooldown ect. for each weapon in a dictionary in a ModuleScript.

2 Likes

I mean how can I use local scripts that the client can’t affect

1 Like

You cannot script a LocalScript that a client cannot affect. What you can do, is to make sanity checks in the clientside, for those normal players that do not exploit. On the server you will then make most if not all the sanity checks again. The reason we make the same sanity checks on the client and the server is due to exploiters.

2 Likes

You should definitely want to use module scripts if you want to reuse code. Local script should be used when you want to run client side code and server script for server side code.

2 Likes

ModuleScripts are good for cleaning clutter, recycling & optimization. It’s good to use modules, especially based upon your hardware because Roblox Studio can become slower depending on the Instance count.

You can see the Instance count using settings().Diagnostics.InstanceCount
Here’s another reference to a reply I made about saving memory. How to fix a data leak - #2 by 04nv

2 Likes

Thats why Im asking when I should use them to affect the client.

What are sanity checks?

1 Like

Example of this could be

--ClientScript
local Banana = "Yellow"
-- exploiter changes banana to "Green"
Remote:FireServer(Banana) -- Now instead of Yellow, and exploiter will send Green
--ServerScript
Remote.OnServerEvent:Connect(function(Player,Banana)
-- If the client changed Banana to nil or away from Yellow, then return end
if not Banana or Banana ~= "Yellow" then return end
-- If client did not change anything, and Banana was equal to Yellow, then run the code below
print("Banana Yellow weee!")
end)
2 Likes

It’s always good to check the class types of a value, common practice with exploiters has been to use table to spoof Instances, which in this case you can always sanity check here, though sanity checking is not really recommended on booleans, numbers & other static values.

Sanity checks go as follows:

local RemoteEvent: RemoteEvent -- Example remote.

RemoteEvent.OnServerEvent:Connect(function(player, value)
	if player.UserId ~= 1 --[[if player is not ROBLOX]] or typeof(value) ~= 'boolean' --[[If value is not true|false]] then
		
	end
end)
2 Likes

Should I make a server script that will check if Banana is changed, and if it did, fire the remote event to another server script?

1 Like

Why?

1 Like

Nonono.

Only make server scripts for new huge tasks.

For example, make a server script dedicated to handling all the playerdata,
Make a server script handling all the block generation
Make a server script handling all the gamepasses & developer products ect. ect.

1 Like

LocalScript → Used for handling & processing data used by a single client. For example, want a door to just open on one persons screen when they click something? Use a LocalScript. In addition, LocalScripts can be used to handle events the server doesn’t need to. For example, even if you are changing the UI on everyone’s screen, use a RemoteEvent that the server uses to fire each client to handle the changes to the UI, instead of the server doing it itself.

ServerScript → You should be using these to handle & replicate data.

ModuleScript → The main idea of a ModuleScript is to reduce the need of rewriting functions. Instead of doing:

function isCandyNice()
return true;
end

In 5 scripts, you can just do:
(ModuleScript)

local module = {}

function module.isCandyNice()
return true;
end

return module;

(Script/LocalScript)

local candyModule = require(PATH_TO_YOUR_MODULE)

candyModule.isCandyNice()
3 Likes