Help with Conveyor system

(Sorry for bad English)
Hi, I would make a game like factorio or mindustry, I made a working conveyor system with miners and furnaces, etc. But here is the problem with the performance is terrible I tried to make optimization to script and I did but still, there is bad performance. I don’t use any loops for this script only script signals but still, the performance is bad. I’m looking at how I can optimize code.
(Server is laggy if are too many items on conveyors)
(Script is in all conveyors in workspace)
My code now :

local item = script.Parent.Item

local tw = game:GetService("TweenService")
local lf = true
local speed = script.Parent["Meshes/uconveyor_obj_conveyor"].Speed
-- System that finds conveyor in front 
local function findconveyor()
	local w = game.Workspace.Machines:GetDescendants()
	local p = script.Parent.Front
	for i,obj in pairs(w) do
		if tostring(obj.Name) == 'Point' and obj.Parent ~= script.Parent  and obj.Parent.Front.Position ~= Vector3.new(p.Parent.Point.Position.X,obj.Position.Y,p.Parent.Point.Position.Z)   then

			if obj.Position == Vector3.new(p.Position.X,obj.Position.Y,p.Position.Z) then
				return obj.Parent
					end
				end
		
	end
end


local mach = game.Workspace.Machines

	local _m 
local a
local a2
local b 
local c
item.Changed:Connect(function()
	if item.Value ~= nil then
		local nextcon = findconveyor()
			-- First try to get conveyor
		if nextcon then
			wait(speed.Value/10)
			if nextcon and item.Value ~= nil then
				if nextcon:FindFirstChild("Item") then
				if  nextcon.Item.Value == nil then
					
			nextcon:WaitForChild("Item").Value = item.Value
			local ti = TweenInfo.new(speed.Value/10,Enum.EasingStyle.Linear,Enum.EasingDirection.In,0,false,0)
			local t_a = tw:Create(item.Value,ti,{CFrame = nextcon.Point.CFrame})
			t_a:Play()
					item.Value = nil

							-- if conveyor is taken already then wait to item go  away	
					else
						if nextcon.Item.Value ~= nil and item.Value ~= nil then
						
							a = 		nextcon.Item.Changed:Connect(function()

								if nextcon.Item.Value == nil then
									
									if nextcon ~= nil  and item.Value ~= nil then
										wait(speed.Value/10)
										if nextcon ~= nil then
										if item.Value ~= nil and nextcon.Item.Value == nil then
											a:Disconnect()
										nextcon:WaitForChild("Item").Value = item.Value
										local ti = TweenInfo.new(speed.Value/10,Enum.EasingStyle.Linear,Enum.EasingDirection.In,0,false,0)
										local t_a = tw:Create(item.Value,ti,{CFrame = nextcon.Point.CFrame})
											t_a:Play()
										item.Value = nil
											end
											end
									else
										a:Disconnect()
									end
								end
							end)
							-- when finded conveyor but is overloaded and destroyed then searching for new conveyor
							b = nextcon.Destroying:Connect(function()
								a:Disconnect()
								b:Disconnect()
								a = nil
								b  = nil
								-- If placed new conveyor then check if item can go to new conveyor
								_m =	mach.ChildAdded:Connect(function (_o)
							
							
									wait(0.5)

									local g = findconveyor()
								

									if g  and  item.Value ~= nil then

										wait(speed.Value/10)
										if g.Item.Value ~= nil then
								

										end
										if g  and  item.Value ~= nil   then
											if g:FindFirstChild("Item") then
												if g.Item.Value == nil then
													local s = 	g:WaitForChild("Item").Value
													g:WaitForChild("Item").Value = item.Value
													local ti = TweenInfo.new(speed.Value/10,Enum.EasingStyle.Linear,Enum.EasingDirection.In,0,false,0)
													local t_a = tw:Create(item.Value,ti,{CFrame = g.Point.CFrame})
													t_a:Play()
													item.Value = nil
													if s == nil then
														_m:Disconnect()
														_m = nil
													end	
												end
											end
										end
									end
								end)
							end)
						
						end
					end
		
				end
				end
		else
			-- If placed new conveyor then checks if item can go to new conveyor
			_m =	mach.ChildAdded:Connect(function (_o)
	
		
				wait(0.5)

				local g = findconveyor()
		
				if g  and  item.Value ~= nil then
					
					wait(speed.Value/10)
					if g.Item.Value ~= nil then
				
					end
					if g  and  item.Value ~= nil   then
						if g:FindFirstChild("Item") then
							if g.Item.Value == nil then
							local s = 	g:WaitForChild("Item").Value
							g:WaitForChild("Item").Value = item.Value
					local ti = TweenInfo.new(speed.Value/10,Enum.EasingStyle.Linear,Enum.EasingDirection.In,0,false,0)
					local t_a = tw:Create(item.Value,ti,{CFrame = g.Point.CFrame})
					t_a:Play()
							item.Value = nil
							if s == nil then
									_m:Disconnect()
									_m = nil
								end	
							end
							end
					end
				end
			end)
			
		end
	end
end)

Maybe I should make a single script for controlling all conveyors?

2 Likes

This script seems pretty complicated for a Conveyor.

A simple conceyor:

script.Parent.Velocity = script.Parent.CFrame.LookVector * 12

Server Script

1 Like

I recently learned about Module Scripts, try those. It’s very good for optimization I heard.

@DasKairo Also, isn’t there like constraints or anything to make a conveyor that doesn’t include scripting?

early is looking like this but is VERY bad for performance

local up = function() 
	while item.Value ~= nil do
wait(speed.Value/10)
		if item.Value  then
			local nextcon = findconveyor()
			if  nextcon ~= nil then
				nextcon.Item.Value = item.Value
				local ti = TweenInfo.new(speed.Value/10,Enum.EasingStyle.Linear,Enum.EasingDirection.In,0,false,0)
				local t_a = tw:Create(item.Value,ti,{CFrame = nextcon.Point.CFrame})
				t_a:Play()
				item.Value = nil
			end
end
	end
end



item.Changed:Connect(function()
	if item.Value ~= nil then
		up()
	end
end)

I think thats the simplest it can get, its just one line of code

1 Like

Maybe he wants it animated / 3D.

You use a beam and 2 Attachments for that

Also here is a ServerScriptService thing:

for _,i in pairs(workspace:GetChildren()) do
	if string.match(i.Name, "C_Slow") then
		i.Velocity = i.CFrame.LookVector * 12
	end
end

The Name C_Slow just stands for Conveyor_Slow

Edit: does not work
Edit 2: Yes it does, i was using the wrong part lol

1 Like

Here is a screen of my game and how it’s looking you can see I have a working system that is setting the position of the item to the next conveyor and again. but If there are too many items server starts to be laggy

I can give a link to game to look how game is working

Send a Video, the image just shows a conveyor and nothing more

Sorry but I can’t my computer is potato

The Script i made should work and may possibly have good performance, you put that script into the ServerScriptService so it will Run on all Parts Named the same as the conveyor instead of a billion seperate scripts. if it has Network issues try :SetNetworkOwner

use something like this
conveyor.AssemblyLinearVelocity = conveyorVelocity

Im pretty sure i just did that :confused:

My Conveyor system is not have moving parts (Moving parts are just Tween) it’s just an object (Conveyor) in a conveyor you have an item for example Stone and the stone is anchored and not moving - moving is made by script by tween and looking for new conveyor then stone position is on new conveyor then conveyor is doing same thing like before conveyor. But I try just do one script in ServerScriptService