I’m not a very organized person and my scripts show it, how can i avoid using nested statements and also keep my scripts more organized for future reference.
Script Example:
while task.wait(0.1) do
if Build1 then
for i, part in pairs(Build1:GetChildren()) do
if part:isA("BasePart") and part.Name ~= "WeldPart" then
if partsOption1 and weldOption1 then
for i, number in pairs(partsOption1) do
local part2 = partsOption1[i]
if part2.Name == part.Name then
local distance = (part2.Parent.PrimaryPart.Position - part.Position).Magnitude
print(distance)
if distance < 5 and part.Name == part.Name then
part.Transparency = 0
part.CanCollide = true
part2:Destroy()
table.remove(partsOption1, i)
weldOption1:Destroy()
weldOption1 = nil
if #PartStoreOption1 == 0 then
MovingTruckProp:Destroy()
CurrentlyBuild = false
Constructing = false
ToolValue = 0
weldOption1 = nil
MovingTruckProp = nil
break
else
print("still good")
end
end
else
print(part2.Name, part.Name)
end
end
end
end
end
end
end
end
From my experience, this stuff is dependent on the problem you’re solving. This structure might be completely necessary for the problem you are solving.
For me, I deal with a lot of loading and unloading instances, and I just use replicated storage because it lets me straight up copy things without having to send and receive and whatever its data, its just right there for me. (otherwise everything I have built would be a massive disaster).
This does not look that bad to me, it looks like there could be another way to approach this with less checks but again, I don’t have the same knowledge as you do about the problem you are attacking…
Practice makes better, and I think practising this problem could make this situation better? By the way, I do not mean you haven’t spent enough time on this. I have been in this exact place after spending hours on a problem.
One thing I would recommend is to convert your nested if-statements into simple return conditions:
For example, heres a bunch of nested if-conditions
if condition1 then
if condition2 then
if condition3 then
if condition4 then
if condition5 then
if condition6 then
--Do some stuff yes
end
end
end
end
end
end
This can keep going and nesting into more layers the more conditions you check, which slowly becomes unreadable and hard to follow. Instead, what we can do is turn each condition check into a return check (or continue check if you are checking this logic inside of a for loop):
if not condition1 then
return
end
if not condition2 then
return
end
if not condition3 then
return
end
if not condition4 then
return
end
if not condition5 then
return
end
if not condition6 then
return
end
--Do some stuff yes
Now we pretty much stay on the same layer while being much more tidy and neater while also still checking those conditions.
This video is pretty much where I learned the Idea, and it has some other neat tips to help organize your if-statements and nesting
That is an excessive amount of nested scopes. Usually, I try to keep my indentations at most 3 long. (From a YouTube video. (Will not post in here since somebody already did.))
You can do the following:
Placing most of your code in functions
Try not to nest everything inside if statements
Either way, this is your code without nested if statements.
while task.wait(0.1) do
if not Build1 then continue end
for i, part in pairs(Build1:GetChildren()) do
if not part:isA("BasePart") or part.Name == "WeldPart" then continue end
if not (partsOption1 and weldOption1) then continue end
for i, number in pairs(partsOption1) do
local part2 = partsOption1[i]
if part2.Name ~= part.Name then print(part2.Name, part.Name) continue end
local distance = (part2.Parent.PrimaryPart.Position - part.Position).Magnitude
print(distance)
if distance >= 5 or part.Name ~= part.Name then continue end -- Wait, why is that here?
part.Transparency = 0
part.CanCollide = true
part2:Destroy()
table.remove(partsOption1, i)
weldOption1:Destroy()
weldOption1 = nil
if #PartStoreOption1 ~= 0 then print("still good") continue end
MovingTruckProp:Destroy()
CurrentlyBuild = false
Constructing = false
ToolValue = 0
weldOption1 = nil
MovingTruckProp = nil
break
end
end
end