Hi developers!
I’m in the process of making scripts for some kind of conveyor belt, storage container and conveyor sorter and… It work really well as it but at some point, I ask myself how can I achieve the same work using a kind of table or anything else instead of writting endlessely so much variables for a single function.
I’m beginner as scripting so, I only figure simple solution who take alot of writting for something, I assume, can be more simple.
The exemple of script I provide in my post is about a “T” shaped conveyor sorter.
- Increment an IntValue per kind of materials entering the “input door”
- Replicate items via 2 exits (“outputLeft” and “outputRight”) as 1 on the left and 1 on the right.
So actually, I’ve wrote a variable and a function per “materials”.
I assume using tables could greatlly improve my script but unfortunatly, I can’t figure out how to use it, even after reading all the documentation on the forum and the Roblox Help.
And finally, is it possible to use only one ModuleScript to drive all the conveyor sorter and storage containers or I have to put that script everytime in every model to make it work as intended?
Can you help me devs?
-------------------
--- Version 1.3 ---
-------------------
local counter = script.Parent.CounterIn
-----------------------------------------------------------------
----MATERIALS COUNTER--------------------------------------------
-----------------------------------------------------------------
--Minerals
local counterCoal = counter.Coal
local counterIron = counter.Iron
local counterCopper = counter.Copper
local counterGold = counter.Gold
local counterTitanium = counter.Titanium
local counterCobalt = counter.Cobalt
--Ingots
local counterIronIngot = counter.IronIngot
local counterCopperIngot = counter.CopperIngot
local counterGoldIngot = counter.GoldIngot
local counterSteelIngot = counter.SteelIngot
local counterTitaniumIngot = counter.TitaniumIngot
local counterCobaltIngot = counter.CobaltIngot
--Plates
local counterIronPlate = counter.IronPlate
local counterCopperPlate = counter.CopperPlate
local counterGoldPlate = counter.GoldPlate
local counterSteelPlate = counter.SteelPlate
local counterTitaniumPlate = counter.TitaniumPlate
local counterCobaltPlate = counter.CobaltPlate
--Pipes
local counterIronPipe = counter.IronPipe
local counterCopperPipe = counter.CopperPipe
local counterGoldPipe = counter.GoldPipe
local counterSteelPipe = counter.SteelPipe
local counterTitaniumPipe = counter.TitaniumPipe
local counterCobaltPipe = counter.CobaltPipe
--Rods
local counterIronRod = counter.IronRod
local counterCopperRod = counter.CopperRod
local counterGoldRod = counter.GoldRod
local counterSteelRod = counter.SteelRod
local counterTitaniumRod = counter.TitaniumRod
local counterCobaltRod = counter.CobaltRod
--Wire
local counterCopperWire = counter.CopperWire
local counterGoldWire = counter.GoldWire
local counterSteelWire = counter.SteelWire
------------------------------------------------------------------
----SERVICES------------------------------------------------------
------------------------------------------------------------------
local replicatedstorage = game:GetService("ReplicatedStorage")
------------------------------------------------------------------
----STORAGE NAME--------------------------------------------------
------------------------------------------------------------------
--store minerals
local coal = replicatedstorage.Coal
local iron = replicatedstorage.Iron
local copper = replicatedstorage.Copper
local gold = replicatedstorage.Gold
local titanium = replicatedstorage.Titanium
local cobalt = replicatedstorage.Cobalt
--store ingots
local ironingot = replicatedstorage.IngotIron
local copperingot = replicatedstorage.IngotCopper
local goldingot = replicatedstorage.IngotGold
local steelingot = replicatedstorage.IngotSteel
local titaniumingot = replicatedstorage.TitaniumIngot
local cobaltingot = replicatedstorage.CobaltIngot
--store plates
local ironplate = replicatedstorage.IronPlate
local copperplate = replicatedstorage.CopperPlate
local goldplate = replicatedstorage.GoldPlate
local steelplate = replicatedstorage.SteelPlate
local titaniumplate = replicatedstorage.TitaniumPlate
local cobaltplate = replicatedstorage.CobaltPlate
--store pipes
local ironpipe = replicatedstorage.IronPipe
local copperpipe = replicatedstorage.CopperPipe
local goldpipe = replicatedstorage.GoldPipe
local steelpipe = replicatedstorage.SteelPipe
local titaniumpipe = replicatedstorage.TitaniumPipe
local cobaltpipe = replicatedstorage.CobaltPipe
--store rods
local ironrod = replicatedstorage.IronRod
local copperrod = replicatedstorage.CopperRod
local goldrod = replicatedstorage.GoldRod
local steelrod = replicatedstorage.SteelRod
local titaniumrod = replicatedstorage.TitaniumRod
local cobaltrod = replicatedstorage.CobaltRod
--store wires
local steelwire = replicatedstorage.SteelWire
local copperwire = replicatedstorage.CopperWire
local goldwire = replicatedstorage.GoldWire
-----------------------------------------------------------------------
----REFERENCES---------------------------------------------------------
-----------------------------------------------------------------------
--output
local leftout = script.Parent.Parent.ContainerOutLeft
local rightout = script.Parent.Parent.ContainerOutRight
--input
local input = script.Parent.Parent.ContainerIn
---------------------------------------------------------------------------------------------------------------------------------
--/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////--
---------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------
----INPUT STORAGE------------------------------------------------------
-----------------------------------------------------------------------
input.Touched:connect(function(hitPart)
if hitPart.Name == "Coal" then
hitPart:Destroy()
counterCoal.Value = counterCoal.Value + 1
end
if hitPart.Name == "Iron" then
hitPart:Destroy()
counterIron.Value = counterIron.Value + 1
end
if hitPart.Name == "Copper" then
hitPart:Destroy()
counterCopper.Value = counterCopper.Value + 1
end
if hitPart.Name == "Gold" then
hitPart:Destroy()
counterGold.Value = counterGold.Value + 1
end
if hitPart.Name == "Titanium" then
hitPart:Destroy()
counterTitanium.Value = counterTitanium.Value + 1
end
if hitPart.Name == "Cobalt" then
hitPart:Destroy()
counterCobalt.Value = counterCobalt.Value + 1
end
if hitPart.Name == "IronIngot" then
hitPart:Destroy()
counterIronIngot.Value = counterIronIngot.Value + 1
end
if hitPart.Name == "CopperIngot" then
hitPart:Destroy()
counterCopperIngot.Value = counterCopperIngot.Value + 1
end
if hitPart.Name == "GoldIngot" then
hitPart:Destroy()
counterGoldIngot.Value = counterGoldIngot.Value + 1
end
if hitPart.Name == "SteelIngot" then
hitPart:Destroy()
counterSteelIngot.Value = counterSteelIngot.Value + 1
end
end)
-----------------------------------------------------------------------
----OUTPUT SORTER------------------------------------------------------
-----------------------------------------------------------------------
local A = true
counterIron.Changed:connect(function()
if counterIron.Value >= 1 then
wait(0.1)
local ingot = iron:Clone()
ingot.Parent = game.Workspace
ingot.Name = "Iron"
if A == true then
ingot.Position = leftout.Position
wait(0.1)
A = false
return end
if A == false then
ingot.Position = rightout.Position
wait(0.1)
A = true
return end
end
end)
local B = true
counterCoal.Changed:connect(function()
if counterCoal.Value >= 1 then
wait(0.1)
local ingot = coal:Clone()
ingot.Parent = game.Workspace
ingot.Name = "Coal"
if B == true then
ingot.Position = leftout.Position
wait(0.1)
B = false
return end
if B == false then
ingot.Position = rightout.Position
wait(0.1)
B = true
return end
end
end)
local C = true
counterCopper.Changed:connect(function()
if counterCopper.Value >= 1 then
wait(0.1)
local ingot = copper:Clone()
ingot.Parent = game.Workspace
ingot.Name = "Copper"
if C == true then
ingot.Position = leftout.Position
wait(0.1)
C = false
return end
if C == false then
ingot.Position = rightout.Position
wait(0.1)
C = true
return end
end
end)
--WAIT... IS THERE A SHORTEST WAY TO DO THAT???