I have this terrible looking code for shopping npcs and was wondering how could I improve it.
-Should a while loop not be used?
-Should I organize variables
-Should I not use loops or should I not have everything nested in a loop.
local CustomerService = coroutine.wrap(function()
while task.wait(1) do
for _, i in pairs(profile.Data.Plot) do
for _, folders in pairs(PlotBuild:GetDescendants()) do
if folders.Name == "CanvasObjects" and folders:IsA("Folder") then
for _, buildings in pairs(folders:GetChildren()) do
if buildings then
if buildings.Name == i then
if itemTypes[i] == "Stands" then
local Stand = buildings
if Stand and Stand.PrimaryPart then
local CustomerSpawn = Plot:FindFirstChild("CustomerSpawn")
if CustomerSpawn:FindFirstChild("Occupied").Value == false then
local worker = Stand.PrimaryPart:FindFirstChild("Worker")
print(worker)
local hasCustomer = worker:FindFirstChild("HasCustomer")
if hasCustomer.Value == false then
hasCustomer.Value = true
local Customer = Npcs.customer:GetChildren()[math.random(1,#Npcs.customer:GetChildren())]:Clone()
Customer.Parent = Stand
if Customer and Stand then
Customer:PivotTo(CFrame.new(CustomerSpawn.Position))
CustomerSpawn:FindFirstChild("Occupied").Value = true
CustomerMOVE(Customer,Stand,plr,Plot)
task.wait(profile.Data.CustomerRate)
CustomerSpawn:FindFirstChild("Occupied").Value = false