Help to use less variables and have more fun!

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???
1 Like

image

Quick mockup example of me using tables to easily cross reference information.

1 Like

Yes, like TechSpectrum suggested, it is very useful to use dictionaries and a for loop so things are more organised. Make sure to use ipairs when looping through arrays or using GetChildren() because i’s faster.

You should also try to utilise elseif statements on your touched event. It’s more organised as well.

It looks like every single variable has the same set materials (coal, iron, copper, gold, titanium, cobalt). You could use string concatenation to identify each one, such as below:

local materials = {
	"Coal",
	"Iron",
	"Copper",
	"Gold",
	"Titanium",
	"Cobalt",
}

for _, mat in ipairs(materials) do
	local Plate = replicatedstorage:FindFirstChild(mat .. "Plate")
	-- more code
end

Nice!! It’s the perfect example I need! :slight_smile:

Is that mean I can store value inside the script instead of using an IntValue per stuff?

Not sure about the meaning of the gameModel variable

local gameModel = things
data.GameModel = gameModel

Thank you!

Oh! Seems to be an awesome way to reduce the amount of variables!
I will try to use it!
using ipairs because it’s an array, of course!
Thank you!

Well… I past the last 2 hours trying to make it work… but still stuck

note:I’m not using any counter atm, just want to make part pass thru
EDIT It work… it work to much!! when a part touch the input, EVERY single part are cloned at the output LOL

local replicatedstorage = game:GetService("ReplicatedStorage")

--output
local leftout = script.Parent.Parent.ContainerOutLeft
local rightout = script.Parent.Parent.ContainerOutRight

--input
local input = script.Parent.Parent.ContainerIn

local A = true

local materials = {
    "Coal",
    "Iron",
    "Steel",
	"Copper",
    "Gold",
    "Titanium",
    "Cobalt",
}

for _, mat in ipairs(materials) do
    local mineral = replicatedstorage:FindFirstChild(mat .. " ")
    local ingot = replicatedstorage:FindFirstChild(mat .. "Ingot")
    local plate = replicatedstorage:FindFirstChild(mat .. "Plate")
    local pipe = replicatedstorage:FindFirstChild(mat .. "Pipe") 
    local rod = replicatedstorage:FindFirstChild(mat .. "Rod")
    local wire = replicatedstorage:FindFirstChild(mat .. "Wire")

    local metalPart = {
	   mineral,
	    ingot,
	    plate,
	    pipe,
	    rod,
	    wire,

    }

for _, mp in ipairs(metalPart) do
    input.Touched:connect(function(hitPart)
		    local copy = mp:Clone()
		    copy.Parent = game.Workspace
		    copy.Name = hitPart.Name
		    hitPart:Destroy()

		    if A == true then
			    copy.Position = leftout.Position
			    wait(0.1)
			    A = false
		    return end
		    if A == false then
			    copy.Position = rightout.Position
			    wait(0.1)
			    A = true
		    return end
	    end)
    end
end

Personally I would make my own enums to represent the type of object and material it’s made from. In Lua, you can easily create an enum by using a dictionary.

For example, you could set them up like this:

local Material = {
	Coal = "Coal",
	Iron = "Iron",
	Steel = "Steel",
	Copper = "Copper",
	Gold = "Gold",
	Titanium = "Titanium",
	Cobalt = "Cobalt"
}

local ObjectType = {
	Mineral = "Mineral",
	Ingot = "Ingot",
	Plate = "Plate",
	Pipe = "Pipe",
	Rod = "Rod",
	Wire = "Wire"
}

This makes them really easy to use throughout your code as you can refer to them by name directly, and if you need to send them between the client and server they’ll work automatically.

1 Like

It’s to store a reference of the stored model in the data table so you don’t need to seek for it in the storage again