Hey guys, so this post is mainly an attempt at improving my overall scripting abilities by applying the best practices. I also hope this post could be used by others who have similar questions to mine.
For Loops
I’ve seen people write for loops in several different ways and am unsure of which of these is the best approach.
for i, v in pairs(model:GetChildren()) do
for _, v in pairs(model:GetChildren()) do
for i, v in (model:GetChildren()) do
(I think this one may error the code but am unsure)
Could someone please explain the differences between these and why one of them might be better than the other.
Random Events
I often find myself writing code with the following lines in it:
local chance = math.random(1,2)
if chance == 1 then
print("Do Event 1")
else
print("Do Event 2")
Is there a more efficient way of doing this? I’ve sometimes had upwards of 15 events and it becomes very tedious to type all of the code out.
Connections
I have never understood connections and still don’t. It seems as though certain lines of code need to be “disconnected” or it could cause issues. I don’t understand this concept though and honestly have no idea how to do it. And how do I know what events need to be disconnected? Like does the .Touched()
event need to be disconnected every time I use it or is it only certain events?
Naming Conventions
I’ve never been entirely sure which naming conventions are the most used by professionals.
Functions:
local function BuyItem()
local function buyItem()
local function buy_item()
Variables:
local killPart = script.Parent
local KillPart = script.Parent
local kill_part = script.Parent
Services:
local ReplicatedStorage = ...
local replicatedStorage = ...
local replicated_storage = ...
UpValues/Constants
I’m honestly kind of clueless as to what these are. I’ve heard a couple developers mention them though. Are they something I need to take into account when writing code?
Remotes
I use RemoteEvents anytime I need to communicate between the client and the server, but am aware there is another instance called a “RemoteFunction.” Can someone explain what the difference is and list a couple examples where a RemoteFunction should be utilized instead of a RemoteEvent?
That’s basically all of my questions regarding basic scripting principles. Any help/feedback is much appreciated! And I apologize in advance if some of my sentences are poorly conveyed/written. It’s very late at night and I’m quite exhausted.