(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?