Having issues converting a table

Hey there folks :smile:

I’ve been trying for the fast few hours to find a work-around to my game system, however I just can’t figure out how to progress from the current stage of my script, as I’m dealing with some table stuff that I just can’t seem to fix

Basically, what I want to achieve, is to get my passed table alligned with another table. So that e.g
a = 1
b = 2
c = 3

but In two tables, and somehow combine it. Currently I have a table that goes 1, 2, 3 and a table that goes ‘‘Part, ‘‘Part’’, Part’’. But I want to allign them like 1 = Part, 2 = Part, 3 = Part (ofc in order)

My Server Script:

--> Services
local Workspace = game:GetService('Workspace')

--> Tables
local Floors_Crafted = {};

--> Modules
local Building_Generator_Module = require(script.BuildingGenerator);
local Config_Module = require(script.Configuration.ConfigModule);

--> Variables
   --> Config Variables
      local Cooldown = Config_Module.LoaderConfig.Setup_Cooldown;

    --> Building_Generator Variables
      local Floors = Config_Module.BuildingConfig.Floors;
      local Floor_Size = Config_Module.BuildingConfig.Floor_Size;
      local Floor_Color = Config_Module.BuildingConfig.Floor_Color;

    --> Other
      local Floor_Part_Position = Workspace:WaitForChild('FloorPartPosition');

--> Functions
local function MakeFloor()
	for i = 1, Floors, 1 do
	local _Object = Building_Generator_Module.CreateObject(

		'Part',
		game.ServerStorage.TempHolder,
		{
			Shape = 'Block',
			Material = 'SmoothPlastic';
			CanCollide = true;
			Anchored = true;
			Transparency = 0;
			Size = Floor_Size;
			Color = Floor_Color;
        }

	);
		table.insert(Floors_Crafted, _Object);
    end;
end;

local function GenerateBuilding()
	task.spawn(MakeFloor)
	print(Floors_Crafted)
	
	Building_Generator_Module.New(
       
		Floors_Crafted,
		Floors,
		Floor_Part_Position.Position
		
     );
end

--> Debug
task.wait(Cooldown)
GenerateBuilding()

My Module script:

Building = {};
Building.__index = Building;

Stored_Floors = {};

local Config_Module = require(script.Parent.Configuration.ConfigModule);
local Floor_Size = Config_Module.BuildingConfig.Floor_Size;

function Building.CreateObject(Class: any, parent: any, properties: any): ObjectValue
	local _Object = Instance.new(Class);
	for index, value in pairs(properties) do
		_Object[index] = value;
	end;
	
	_Object.Parent = parent;
	return _Object;
end;

function Building.New(FloorPart: any, Floors: number, Position: Vector3)
	local Floor_Part_Position = Position
	local Bottom_Floor = nil;
	
	local Initial_Part_Y = Floor_Part_Position.Y;
	local Floor_Part_Y = Floor_Size.Y / 2;
	
	local Bottom_Position = (Floor_Part_Position + Vector3.new(0, Initial_Part_Y, 0)) + Vector3.new(0, Floor_Part_Y, 0);
	
	local New_Building = {};
	setmetatable(New_Building, Building);
	
	local Floor_Amount = Floors;
	for i = 1, Floor_Amount do
		table.insert(Stored_Floors, i);
	end;
	
	for _, Floor in ipairs(Stored_Floors) do
		task.wait(1)
		FloorPart.Parent = game.Workspace;
		if #Stored_Floors > Floors then
			warn('Error when getting the total amount of floors');
		else
			if Bottom_Floor == nil and Floor == 1 then
				Bottom_Floor = 1;
				FloorPart.Position = Bottom_Position;
			else
				if Floor > Bottom_Floor then
				   FloorPart.Position = FloorPart.Position + Vector3.new(0, Floor_Part_Y * 2, 0);
				end;
			end;
		end;
	end;

	return New_Building;
end;

return Building; 

My config Script

local Config = {
     BuildingConfig = {
		Floors = 3;
		Floor_Size = Vector3.new(50, 4, 37);
		Floor_Color = Color3.fromRGB(255, 142, 144);
    };
     LoaderConfig = {
		Setup_Cooldown = 3;
    };
};

return Config;

Thanks for the help!

1 Like

From your description, I’m assuming you want to combine the elements of two tables into a singular one, if so, you would have to implement this sort of algorithm, where you give a name to an element of table(3) and assign it a value.

Below is an example of the algorithm described:

local table1 = {
	1,
	2,
	3
}

local table2 = {
	"Part1",
	"Part2",
	"Part3"
}

local table3 = {}

for index, element in table1 do
	
	table3[element] = table2[index]
	
end

print(table3)

The resulting output is similar to what you are trying to achieve in your description, albeit the algorithm would have to be modified to fit within your system as the one I’ve given is just an example.

[1] = "Part1",
[2] = "Part2",
[3] = "Part3"
1 Like

Thank you for providing this. I’ll have to go try implement it to fix my code up, aswell as this post since the format is all wrong and looks terrible. I guess it’s hard to say when devforum randomly goes down for 10 hours :sad:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.