Question about scripting on how to start clean and create some sort of framework?

Hello,

I would like to start scripting in roblox again. Ive prevously started but it was more like repairing existing scripts and editing existing scripts but i dont wanna do that anymore. I would like to try create a simple game at first. I quickly changed to garrys mod but wanna slowly come back to roblox as ive seen the update posts in roblox and i actually like in what direction roblox finaly goes and this anti cheat thing aswell.

Now lets come to my question.

I would like to create a simple “city life” roleplay game and for that i would like to script my own “framework”.

In garrys mod you are able to create a own gamemode and as example create a serversided script that has global playermeta functions as example
– the script can be called sv_money.lua
then it goes like

gmod code example
function meta:SetMoney(amount)
	if not self.beenSetup or self.beenSetup == false then return end
	if not isnumber(amount) or amount < 0 or amount >= 1 / 0 then return end

	local query = mysql:Update("impulse_players")
	query:Update("money", amount)
	query:Where("steamid", self:SteamID())
	query:Execute()

	return self:SetLocalSyncVar(SYNC_MONEY, amount)
end

function meta:SetBankMoney(amount)
	if not self.beenSetup or self.beenSetup == false then return end
	if not isnumber(amount) or amount < 0 or amount >= 1 / 0 then return end

	local query = mysql:Update("impulse_players")
	query:Update("bankmoney", amount)
	query:Where("steamid", self:SteamID())
	query:Execute()

	return self:SetLocalSyncVar(SYNC_BANKMONEY, amount)
end

function meta:GiveBankMoney(amount)
	return self:SetBankMoney(self:GetBankMoney() + amount)
end

function meta:TakeBankMoney(amount)
	return self:SetBankMoney(self:GetBankMoney() - amount)
end

function meta:GiveMoney(amount)
	return self:SetMoney(self:GetMoney() + amount)
end

function meta:TakeMoney(amount)
	return self:SetMoney(self:GetMoney() - amount)
end```

and the good thing about that is that i can in another script just use the function GiveMoney when i have another script that should give the player money.

And my question is can you create multiple server scripts that each has its own purpose and use those functions inside globally somewhere else in another script ?.

Cause when i already wanna start my own project from the ground up i want to script clean as possible so i dont encounter no updating hassle

1 Like

ModuleScripts :slight_smile:


I believe you are referring to the wonderous ModuleScript.

I use ModuleScripts for just about all of my code because they allow reusability and accessible storage of values. In order to use ModuleScripts you’ll need knowledge of OOP, scope, and dependency; but by the looks of the GMOD code it appears you have those things covered.

In a project I’m currently working on I have a single script for the client and a single script for the server. Inside these scripts are about a dozen or so module scripts that I control from the server or client.

I also have a “supermodule” in ReplicatedStorage that contains functions that can be used by both the server and the client.


Screenshot Examples:

Server:

image


Client:

image


Replicated:

image


Usage Example:

image
image

I’m going to use one of the simpler modules as an example. Inside the Client script, I have a module named Lamp.

Lamp is basically a module I use to configure and initialize all of the lanterns on the client. Doing lighting and particles on the client is preferred since it takes up less server memory and allows clients to toggle them more easily.


This is the code inside ‘Lamp’:

local CS = game:GetService("CollectionService")

local Lamp = {}

function Lamp.InitializeAll()
	for _,lanternPost in ipairs(CS:GetTagged("LanternPost")) do
		local postAtt = lanternPost.Post.PrimaryPart.PostAttachment
		local lanternAtt = lanternPost.Lantern.PrimaryPart.LanternAttachment

		local chain = script.LanternPost.Chain:Clone()
		chain.Attachment0 = postAtt
		chain.Attachment1 = lanternAtt
		chain.Parent = lanternPost

		local light = script.LanternPost.PointLight:Clone()
		light.Parent = lanternPost.Lantern.Light

		local particles = script.LanternPost.Particles:Clone()
		particles.Parent = lanternPost.Lantern.Metal
	end
end

return Lamp

Here is the related code inside Client’:

local Lamp = require(script.Lamp)

Lamp.InitializeAll()

As you can tell, this is a very simple example of calling a function that is stored inside a ModuleScript. The Lamp.InitializeAll() function is called when the player joins because the Client LocalScript is inside StarterPlayerScripts.


Module Values:

As for storing values, you can simply do on the ModuleScript Lamp.Brightness = 0 and then on the client do print(Lamp.Brightness) assuming you did local Lamp = require(script.Lamp) on the client.


Cross-Module Communication:

You can also communicate data between ModuleScripts! Like if you had a module that controls money you could send values over to your housing module to do a condition check for whether or not a player can afford the house. To do this you would simply require(pathToModule) the module where you need it.

I hope this helps! :slight_smile:

3 Likes

I see you use the tag feature and run a ipairs loop everything that has the tag LanternPost. Yea nice
i never knew module scripts can be used for this. The only instance i saw module scripts in was in gun scripts to change weapon stats thats it.

What about exploiter accessbility ? can they just play around with values inside modul scripts ?

1 Like

Yes, all global values inside of client-sided ModuleScripts are visible to exploiters. If the value is in the local scope or is stored on the server it should be safe.


Global / Local:

A global value in a ModuleScript is denoted by moduleName.valueName = value
A local value in a ModuleScript is denoted by local valueName = value


For example:

local module = {}

--// Can be read by other modules and scripts (not exploiter safe!)
module.OurGlobalValue = 10

--// Cannot be read by other modules and scripts (exploiter safe!)
local ourLocalValue = 10

return module

And our command-line output to show it is global:

Ignoring my goofy output font, you can see the command I ran in the command line of Roblox Studio in line 1 and the output in line 2. Global values are not safe; but they can be used if you’re simply using them for visuals such as brightness.


The example I gave earlier, Lamp.Brightness = 0, would be visible to exploiters in the sense that they can do local Lamp = require(game.Players.Xitral.Client.Lamp); Lamp.Brightness = 10; in a command line or with a script executor. They won’t see it in the explorer when using something like DarkDex so it’s safer than using regular value instances.


Safety:

If you want something to be safe from an exploiter, you should keep the values local and just have global functions. And maybe have global functions that set and read values too! You should try to keep most of your code on the server when possible. Exploiters can only alter the server through RemoteEvents and RemoteFunctions.

alright so its still the same that they can only mostly abuse RemoteEvents and RemoteFunctions, alright gotchu, thanks for the information. Good thing that synapse is down that stops literlly like 80% of exploiters rn

1 Like

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