Need help optimizing this goofy script

Its hard to fully explain what this function does, but I basically need help optimizing the part that stores Objects in an array based off of the floor and number values. I can explain the script if needed, its just a challenge and I’m too tired to type how it works here

local function AddRoom(player, eventBunkerNum, roomType, replacedDirt, roomFloor, roomNumber) --Fires from remote event with variables
	if eventBunkerNum == bunkerNum then
		replacedDirt:Destroy()
		local newRoom = game.ReplicatedStorage.RoomModels:WaitForChild(roomType):Clone()
		newRoom.PrimaryPart.Position = Vector3.new(0, (roomFloor) * 15, roomNumber * -30)
		newRoom.Name = roomType..roomFloor..roomNumber
		newRoom.Parent = script.Parent
        --Ignore eveything up to here
		if roomFloor + 1 > floorNumber then
			local difference = roomFloor + 1 - floorNumber
			for count = 1, difference do
				table.insert(rooms, {})
				table.insert(rooms[floorNumber + count], 0)
			end
			floorNumber += difference
		end
		if rooms[roomFloor + 1][1] < roomNumber - 1 then
			local difference = roomNumber - 1 - rooms[roomFloor + 1][1]
			print(difference)
			for count = 1, difference do
				table.insert(rooms[roomFloor + 1], "none")
				rooms[roomFloor + 1][1] += 1
			end
		end
		if rooms[roomFloor + 1][1] > roomNumber then
			rooms[roomFloor + 1][roomNumber + 1] = roomType
		else
			table.insert(rooms[roomFloor + 1], roomType)
			rooms[roomFloor + 1][1] += 1
		end
        --Also ignore eveything after this
		print(rooms)
		print(roomFloor.." "..roomNumber)
		wait(0.1)
		for _, part in pairs(newRoom:GetChildren()) do
			if part:IsA("BasePart") and part.Name ~= "Primary" then
				part:WaitForChild("Weld"):Destroy()
				part.Anchored = true
			end
		end
	end
end
1 Like

This code really only shortens the conditionals/logic down and replaces things like wait with task.wait and instead of using “in pairs” which you dont need to do anymore so that was removed. The code may or may not work, im unsure without being able to test it.

local function AddRoom(player, eventBunkerNum, roomType, replacedDirt, roomFloor, roomNumber)
    if eventBunkerNum ~= bunkerNum then return end
    replacedDirt:Destroy()

    local newRoom = game.ReplicatedStorage.RoomModels:WaitForChild(roomType):Clone()
    newRoom.PrimaryPart.Position = Vector3.new(0, roomFloor * 15, roomNumber * -30)
    newRoom.Name = roomType .. roomFloor .. roomNumber
    newRoom.Parent = script.Parent

    floorNumber = math.max(floorNumber, roomFloor + 1)

    local roomFloorData = rooms[floorNumber]
    local roomNumberData = roomFloorData[1]

    for count = 1, math.max(0, roomNumber - 1 - roomNumberData) do
        table.insert(roomFloorData, "none")
        roomFloorData[1] = roomNumber
    end

    roomFloorData[roomNumber + 1] = roomNumberData > roomNumber and roomType or table.insert(roomFloorData, roomType)
    print(rooms)
    print(roomFloor .. " " .. roomNumber)
    task.wait(0.1)

    for _, part in (newRoom:GetChildren()) do
        if part:IsA("BasePart") and part.Name ~= "Primary" then
            part:WaitForChild("Weld"):Destroy()
            part.Anchored = true
        end
    end
end

Yeah sorry I couldn’t explain more, but most of the optimizations are fine and I just had to change a few things to get it to work properly again. I probably should’ve said that the table doesn’t actually have values for all the floors inside of it and I have to create them which I think was what didn’t happen here, but I’m not entirely sure. Anyways thank you, it looks a lot cleaner now.

Here is the optimized code if somebody needs it for some reason:

local function RoomCreator(player, eventBunkerNum, roomType, replacedDirt, roomFloor, roomNumber)
	if eventBunkerNum ~= bunkerNum then return end
	replacedDirt:Destroy()
	
	local newRoom = game.ReplicatedStorage.RoomModels:WaitForChild(roomType):Clone()
	newRoom.PrimaryPart.Position = Vector3.new(0, (roomFloor) * 15, roomNumber * -30)
	newRoom.Name = roomType..roomFloor..roomNumber
	newRoom.Parent = script.Parent
	
	local floorDifference = roomFloor + 1 - floorNumber
	for count = 1, floorDifference, 1 do
		table.insert(rooms, {0})
	end
	floorNumber += math.clamp(floorDifference, 0, math.abs(floorDifference))
	
	local roomDifference = roomNumber - rooms[roomFloor + 1][1]
	if rooms[roomFloor + 1][1] < roomNumber - 1 then
		for count = 1, roomDifference, 1 do
			table.insert(rooms[roomFloor + 1], "none")
		end
	end
	rooms[roomFloor + 1][1] += math.clamp(roomDifference, 0, math.abs(roomDifference))
	
	rooms[roomFloor + 1][roomNumber + 1] = roomType
	task.wait(0.1)
	for _, part in pairs(newRoom:GetChildren()) do
		if part:IsA("BasePart") and part.Name ~= "Primary" then
			part:WaitForChild("Weld"):Destroy()
			part.Anchored = true
		end
	end
end

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