Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Is it best to have many lines of codes in one script or less lines throughout many scripts? [closed]

Asked by 5 years ago

This mainly applies to LocalScripts, having one script to handle everything client I believe is easier for me as I am able to access other variables within that one script rather than having two scripts where they need to communicate using BindableEvents (where a delay could be evident), this is what my skills unfortunately land.

Could someone explain to me which practice out of the two is best and why, and if many scripts are to be involved within the client, how do I transfer data such as variables across them.

0
You want to make sure that one thing is doing one logical task as to not duplicate code or make one script a "multi tool" User#5423 17 — 5y

Locked by User#24403

This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.

Why was this question closed?

1 answer

Log in to vote
5
Answered by 5 years ago
Edited 5 years ago

Question

Is it best to have many lines of codes in one script or less lines throughout many scripts?

My Answer

(incoming lengthy post; uncopylocked place at the end if you want to look at it.)

Well this post feels more ambiguous to me so excuse me if I make any mistakes while explaining an answer to your problem.

Using big massive scripts that do everything on your client side is generally not a good idea. I usually split my code up into several modules and the main client requires those modules so I can easily maintain my code. Also if you put all your game code in one script, if one thing happens to break its likely the entire game will become unplayable.

Generally if you're going to use the same chunk of code more than once such as an NPC dialog handler you would want to use modules. Modules are very useful and can return any datatype you throw into them. Functions, strings, tables, and numbers. If you put the same script in 10 different places and it ends up not working you have to fix all of those scripts. This would be a problem especially if you're writing a gun system and all of your guns happen to stop working and you had to edit all of their scripts.

Example

Lets go through a code example and in this case we will be showing how to easily maintain our code. For this example we will be using a NPC dialog system that randomly says something in our dialogChoices table. We will put a single script in every npc that has the same functions except their dialog table is different.

Case Example Code

--// NPC Example

local chatService = game:GetService("Chat")

local npc = script.Parent
local head = npc:WaitForChild("Head")

local dialogChoices = {
    "hello there.",
    "who are you?",
    "this is cool!"
}

local function respond(text)
    chatService:Chat(head, text, Enum.ChatColor.Blue)
end

local function randomResponse()
    local response = dialogChoices[math.random(1, #dialogChoices)]

    respond(response)
end

while true do
    randomResponse()
    wait(1)
end

Case Problem

Now image we had 100 of these NPC's and they all use the same text. Imagine we want to add a new dialog choice to all of the npcs. We would have a problem where we have to edit 100 scripts which will be a lengthy amount of time.

Case Answer

In each individual NPC dialog script we can require a module that returns randomResponse function that is able to call our respond function and we can also put our dialogChoices in there.

Example Module Code

local chatService = game:GetService("Chat")

local dialogChoices = {
    "hello there.",
    "who are you?",
    "this is cool!"
}

local function respond(part, text)
    chatService:Chat(part, text, Enum.ChatColor.Blue)
end

local function randomResponse(npc)
    local response = dialogChoices[math.random(1, #dialogChoices)]
    local head = npc:WaitForChild("Head")

    respond(head, response)
end

return randomResponse

Now with most of the dialog code in our module we can require this module from our 100 npc handler scripts and we can change the dialog choices for every single npc by just editing 1 script! (The dialogChoices are now in the newModule so we would edit there)

Example NPC Code

local serverStorage = game:GetService("ServerScriptService")

local modules = serverStorage:WaitForChild("Modules")
local randomResponse = require(modules:WaitForChild("RandomResponse"))

local npc = script.Parent

while true do
    randomResponse(npc)
    wait(1)
end

Another Problem Arises

Now you may be asking something along the lines of "What if I want to edit something specifically in the serverscript that handles the NPC dialog that we can't put in our module"

Answer To New Problem

We can put all our NPC's in a folder or a model in workspace. In this case since only NPC's are in there we can name it "NPCs"

After we put them all in a folder we can have a script in serverstorage that we clone into all of the NPC's. Before run time we can edit that single script and basically only have 3 scripts to control everything. We have maintained our code hopefully well.

--// Example code for cloning `dialogHandler` into npcs.

local serverStorage = game:GetService("ServerStorage")
local dialogHandler = serverStorage:WaitForChild("DialogHandler")

local NPCs = workspace:WaitForChild("NPCs")

for i, npc in next, NPCs:GetChildren() do
    dialogHandler:Clone().Parent = npc
end

Extra

Please accept this answer if it helped you. We both will benefit since I can know your question was solved as well my rep being boosted so hopefully people come for me to help!

Here is an uncopylocked place with all the code we wrote. You can mess around with it here. https://www.roblox.com/games/2657041100/NPC-Testing-SH-Question

0
Never seen answer like that ???????????????? HaveASip 494 — 5y
0
This is a very detailed and well answer, and answers questions I was thinking about, so you can use ModuleScripts even for LocalScripts? I tend to use them for ServerScripts Marmalados 193 — 5y
Ad