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
03 | local chatService = game:GetService( "Chat" ) |
05 | local npc = script.Parent |
06 | local head = npc:WaitForChild( "Head" ) |
08 | local dialogChoices = { |
14 | local function respond(text) |
15 | chatService:Chat(head, text, Enum.ChatColor.Blue) |
18 | local function randomResponse() |
19 | local response = dialogChoices [ math.random( 1 , #dialogChoices) ] |
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
01 | local chatService = game:GetService( "Chat" ) |
03 | local dialogChoices = { |
09 | local function respond(part, text) |
10 | chatService:Chat(part, text, Enum.ChatColor.Blue) |
13 | local function randomResponse(npc) |
14 | local response = dialogChoices [ math.random( 1 , #dialogChoices) ] |
15 | local head = npc:WaitForChild( "Head" ) |
17 | respond(head, response) |
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
01 | local serverStorage = game:GetService( "ServerScriptService" ) |
03 | local modules = serverStorage:WaitForChild( "Modules" ) |
04 | local randomResponse = require(modules:WaitForChild( "RandomResponse" )) |
06 | local npc = script.Parent |
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.
03 | local serverStorage = game:GetService( "ServerStorage" ) |
04 | local dialogHandler = serverStorage:WaitForChild( "DialogHandler" ) |
06 | local NPCs = workspace:WaitForChild( "NPCs" ) |
08 | for i, npc in next , NPCs:GetChildren() do |
09 | dialogHandler:Clone().Parent = npc |
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
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?