Hey, so I’ll give you an example of what my typical process is for creating code.
So say for example, in a UI, we want to gather all the buttons and initiate events on them that detect when they’ve been pressed, maybe to close the frames that they’re in (close buttons.) I would create a new for loop in my client code that does exactly that. After I do, maybe I want to add in functionality for each frame that I have. For example, a character customizer frame where you can customize your avatar by selecting different items. For that, I’d generally add in more client code, define more variables, to achieve this. And so on.
I also try to add commenting to break up sections of my code so I know which chunk does what.
However, I’m curious as to if there’s a better method of structuring your code, specifically client-code in this given scenario. I’ve always compiled whatever functions I want to achieve client-sided in a single script, with occasional module scripts if I feel like a certain component of the UI is larger. Like, a character customizer.
I hope this makes sense, if not I can clarify a bit. Just looking for advice in terms of how to actually structure a scenario like this in a cleaner fashion. Thanks!
2 Likes
The Importance of structuring code is to give the programmer a better idea of what’s going on. Comments and scoping are great examples of structuring code, which I use nearly every time I code.
3 Likes
I think this is more of an opinion related thread, because people like writing code in different ways. Here is how I structure my code:
Firstly, I define variables:
-
GetService()
variables
local PS = game:GetService("Players")
local SS = game:GetService("ServerStorage")
local RP = game:GetService("ReplicatedStorage")
- Service descendants (notice how the order of referencing the children of the services is the order that I referenced the services)
local PlayerTable = Players:GetPlayers()
local Tower = SS:WaitForChild("Tower")
local Button = RP:WaitForChild("ImageButton")
local Remote = RP:WaitForChild("RemoteEvent")
- Other variables
local Script = script.Parent
local Bomb = Script.Bomb
Secondly, I would write the layout of the code(sometimes this is the last step):
--[ Items Handler ]-- <--(This is a category)
--[ Bomb <--(this is a sub-category)
--[ Events ]--
--[ Remote Events
--[ Other ]--
Lastly, I write the code
local Cloned = Bomb:Clone()
Cloned.Parent = workspace
Cloned.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
hit.Parent.Humanoid:TakeDamage(100)
end
end)
Remote.OnServerEvent:Connect(function(player, Text)
print(Text)
end)
Script.Name = "Hello World!"
The final layout of the script would look something like this:
local PS = game:GetService("Players")
local SS = game:GetService("ServerStorage")
local RP = game:GetService("ReplicatedStorage")
local PlayerTable = Players:GetPlayers()
local Tower = SS:WaitForChild("Tower")
local Button = RP:WaitForChild("ImageButton")
local Remote = RP:WaitForChild("RemoteEvent")
local Script = script.Parent
local Bomb = Script.Bomb
--[ Items Handler ]--
--[ Bomb
local Cloned = Bomb:Clone()
Cloned.Parent = workspace
Cloned.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
hit.Parent.Humanoid:TakeDamage(100)
end
end)
--[ Events ]--
--[ Remote Events
Remote.OnServerEvent:Connect(function(player, Text)
print(Text)
end)
--[ Other ]--
Script.Name = "Hello World!"
6 Likes