Auto Arranging Roads

i tried making a auto-arranging road system but it does not seem to work at all

the function:

local function arrangeRoad(road: Model)
	local function getSurroundingNeighbours()
		local size = road:GetExtentsSize()
		local pos = road:GetPivot().Position

		local data = {
			Vector3.new(pos.X, pos.Y, pos.Z + size.Z); -- a
			Vector3.new(pos.X + size.X, pos.Y, pos.Z); -- b
			Vector3.new(pos.X, pos.Y, pos.Z - size.Z); -- c
			Vector3.new(pos.X - size.X);               -- d
		}
		
		local roadIndex = 0
		local roadsData = {'a', 'b', 'c', 'd'}
		
		local roads = {
			a = false;
			b = false;
			c = false;
			d = false;
		}
		
		print(road.Parent:GetFullName())
		print(road.Parent:GetChildren())
		for _, v in road.Parent:GetChildren() do
			print(v)
			local index = table.find(data, v:GetPivot().Position)
			
			print(index)
			print(roadIndex)
			if index then				
				roadIndex += 1
				roads[roadsData[roadIndex]] = v
			end
		end
		
		return roads
	end
	
	local neighbours = getSurroundingNeighbours()
	local recipe = ''
	
	for _, v in neighbours do
		if v then
			recipe ..= '1'
		else
			recipe ..= '0'
		end
	end
	
	print(recipe)
	
	local appropriateRoad = ServerStorage.Roads[road.Name]:FindFirstChild(recipe)
	
	if appropriateRoad then
		local clone = appropriateRoad:Clone()
		clone:PivotTo(road:GetPivot())
		clone.Parent = road.Parent
		
		road:Destroy()
	end
end

(based on Tile Scrolling Platformer | 12. Auto Arranging Tiles - YouTube)

figured that the roads table are broken but its still not working

local function arrangeRoad(road: Model)
	local function getSurroundingNeighbours()
		local size = road:GetExtentsSize()
		local pos = road:GetPivot().Position

		local data = {
			Vector3.new(pos.X, pos.Y, pos.Z + size.Z); -- a
			Vector3.new(pos.X + size.X, pos.Y, pos.Z); -- b
			Vector3.new(pos.X, pos.Y, pos.Z - size.Z); -- c
			Vector3.new(pos.X - size.X);               -- d
		}
		
		local roadsData = {'a', 'b', 'c', 'd'}
		
		local roads = {}
		
		for _, v in road.Parent:GetChildren() do
			local index = table.find(data, v:GetPivot().Position)
			
			if index then
				roads[index] = v
			end
		end
		
		return roads
	end
	
	local neighbours = getSurroundingNeighbours()
	local recipe = ''
	
	for i = 1, 4 do
		local v = neighbours[i]
		if v then
			recipe ..= '1'
		else
			recipe ..= '0'
		end
	end
	
	print(recipe)
	
	local appropriateRoad = ServerStorage.Roads[road.Name]:FindFirstChild(recipe)
	
	if appropriateRoad then
		local clone = appropriateRoad:Clone()
		clone:PivotTo(road:GetPivot())
		clone.Parent = road.Parent
		
		road:Destroy()
	end
end

somewhat working now? (i called arrangeRoad to every single road that has been placed)

local function arrangeRoad(road: Model)
	local function getSurroundingNeighbours()
		local size = road:GetExtentsSize()
		local pos = road:GetPivot().Position

		local data = {
			Vector3.new(pos.X, pos.Y, pos.Z + size.Z); -- a
			Vector3.new(pos.X + size.X, pos.Y, pos.Z); -- b
			Vector3.new(pos.X, pos.Y, pos.Z - size.Z); -- c
			Vector3.new(pos.X - size.X);               -- d
		}
		
		local roadsData = {'a', 'b', 'c', 'd'}
		
		local roads = {}
		
		for _, v in road.Parent:GetChildren() do
			local index = table.find(data, v:GetPivot().Position)
			
			if index then
				roads[index] = v
			end
		end
		
		return roads
	end
	
	local neighbours = getSurroundingNeighbours()
	local recipe = ''
	
	for i = 1, 4 do
		local v = neighbours[i]
		if v then
			recipe ..= '1'
		else
			recipe ..= '0'
		end
	end
	
	print(recipe)
	
	local appropriateRoad = ServerStorage.Roads:FindFirstChild(recipe)
	
	if appropriateRoad then
		local clone = appropriateRoad:Clone()
		clone:PivotTo(CFrame.new(road:GetPivot().Position))
		clone.Parent = road.Parent
		
		road:Destroy()
	end
end

1 Like