HELP to organize CODE better!


local function cleanStainsFires()
	if workspace:FindFirstChild("Stains") then 
		workspace.Stains:Destroy()
	end
	if workspace:FindFirstChild("Fires") then 
		workspace.Fires:Destroy()
	end
end

--TRUCK JOB
local canCarrySupply = true

local function showRandomTarget(toggle)
	if toggle == true then
		local getRandomTarget = MallSupplier.truckShops:GetChildren()[math.random(1,#MallSupplier.truckShops:GetChildren())]

		local helper = getRandomTarget.Info
		local clicker = getRandomTarget.Clicker

		getRandomTarget.Transparency = 0

		helper.Enabled = true
		clicker.Enabled = true

		clicker.Triggered:Connect(function(player)
			if canCarrySupply == false then
				canCarrySupply = true
				--invisible the item
				helper.Enabled = false
				clicker.Enabled = false
				getRandomTarget.Transparency = 1
				--give player cash

				jobRemotes:WaitForChild("clientEarnJobMoney"):FireServer("Mall Supplier")

				--after all finished, return success
			end
		end)
	else
		for t,k in pairs(MallSupplier.truckShops:GetChildren()) do
			k.Info.Enabled = false
			k.Clicker.Enabled = false
			k.Transparency = 1
		end
	end
	
	--if finishedJob == true then
	--	--finishedJob = false
	--	return "Success" 
	--end
end


local function handleTruck(toggle)
	if toggle == true then
		for index,value in pairs(mallTrucks:GetChildren()) do
			local infoCard = value:FindFirstChild("Info",true)
			local clicker = value:FindFirstChild("clicker",true)
			local supplyAmount = value:FindFirstChild("SupplyAmount",true)
			local amountText = value:FindFirstChild("jobInfo",true).gui.amountText
			
			clicker.Enabled = true
			
			--Destroying the truck
			supplyAmount.Changed:Connect(function(newv)
				amountText.Text = newv.."/10"
				if newv == 0 then
					local getTruck = supplyAmount.Parent
					getTruck.Truck.Transparency = 1
					getTruck.Truck.CanCollide = false
					getTruck.Truck.Info.Enabled = false
					getTruck.getSupply.clicker.Enabled = false
					getTruck.getSupply.gui.Enabled = false
					getTruck.jobInfo.gui.Enabled = false
					
					spawn(function()
						task.wait(40)
						if CurrentJob.Value == "Mall Supplier" then
							getTruck.Truck.Transparency = 0
							getTruck.Truck.CanCollide = true
							getTruck.Truck.Info.Enabled = true
							getTruck.getSupply.clicker.Enabled = true
							getTruck.getSupply.gui.Enabled = true
							getTruck.jobInfo.gui.Enabled = true
						end
						getTruck.SupplyAmount.Value = 10
					end)
				end
			end)
			
			clicker.Triggered:Connect(function(player)
				print(canCarrySupply)
				if supplyAmount.Value > 0 then
					if canCarrySupply then
						canCarrySupply = false
						supplyAmount.Value -= 1
						showRandomTarget(true)
						--local getResult = showRandomTarget()
						--if getResult == "Success" then
						--	canCarrySupply = true
						--end
					end
				end
			end)	
			infoCard.Enabled = true
		end
	else
		for index,value in pairs(mallTrucks:GetChildren()) do
			local infoCard = value:FindFirstChild("Info",true)
			local clicker = value:FindFirstChild("clicker",true)

			infoCard.Enabled = false
			clicker.Enabled = false
		end
		canCarrySupply = true
		showRandomTarget(false)
	end
end
-- TRUCK JOB


leaderstats.CurrentJob.Changed:Connect(function(val)
	if val == "Mall Supplier" then
		cleanStainsFires()
		handleTruck(true)
		
	elseif val == "Street Cleaner" then
		local stains = jobStuff:WaitForChild("Stains"):Clone()
		handleTruck(false)
		if workspace:FindFirstChild("Stains") then return end
		stains.Parent = workspace
		
		if workspace:FindFirstChild("Fires") then 
			workspace.Fires:Destroy()
		end
	elseif val == "FDNY" then
		local stains = jobStuff:WaitForChild("Fires"):Clone()
		handleTruck(false)
		if workspace:FindFirstChild("Fires") then return end
		stains.Parent = workspace
		if workspace:FindFirstChild("Stains") then 
			workspace.Stains:Destroy()
		end
	else
		handleTruck(false)
		cleanStainsFires()
	end
end)

if CurrentJob.Value == "Street Cleaner" then
	local stains = jobStuff:WaitForChild("Stains"):Clone()
	if workspace:FindFirstChild("Stains") then return end
	stains.Parent = workspace
elseif CurrentJob.Value == "FDNY" then
	local stains = jobStuff:WaitForChild("Fires"):Clone()
	if workspace:FindFirstChild("Fires") then return end
	stains.Parent = workspace
elseif CurrentJob.Value == "Mall Supplier" then
	handleTruck(true)
end


local script

4 Likes

Won’t exactly help organizing but for the love of god use multi-line comments instead of commenting each line singularly.
Instead of this:

--local getResult = showRandomTarget()
--if getResult == "Success" then
--	canCarrySupply = true
--end

you can do this:

--[[local getResult = showRandomTarget()
if getResult == "Success" then
	canCarrySupply = true
end--]]
1 Like

Also something to add, is that I recommend disconnecting connections that are in functions or ones that are inside other connections after they are used.

I am not sure if you intend to keep them, and it is unlikely that they will create any major issues, but it is good practice regardless.

1 Like

To be honest, what the author did is probably better because multiple lines of comments can easily be toggled with “Ctrl + /”. The notation for multi-line commenting isn’t compatible with that shortcut.

1 Like

You could use modules to clean things up, using PascalCase will make the code more visually appealing (although entirely personal preference), and adding comments will help organize things.

For example: this is a snippet of how I organize my code (ModuleScript)