Room Generation doesn't generate anything

Its basically the title, but I’ll give you some more insight.
The code is supposed to for every connection point a model has generate a dedicated (random) room for it, and then destroy the point. And with all of that said, it doesn’t work. Anyone know why?

script located in serverscriptservice:

local roomsLimit = 100
local GenerateDelay = true
local RoomsGenerating = 0
local CurrentNum = 1

--Setups
--+ Changeable
local MaximumRooms = 100
local MinimumRooms = 1

local function RarityFunction(Rooms)
	
	local Weight = 0
	
	for _, Room in pairs(Rooms:GetChildren()) do
		
		Weight += Room.Rarity.Value
		
	end
	
	local randomWeight = Random.new():NextInteger(1, Weight)
	local currentWeight = 0
	local randomRoom = nil
	for	i, Room in pairs(Rooms:GetChildren()) do
		
		currentWeight += Room.Rarity.Value
		if randomWeight <= currentWeight then
			
			randomRoom = workspace.GasPipeRooms[i]
			break
			
		end
		
	end
	
end

local function GenerateRoom(PrevRoom)
	
	for i, v in pairs(PrevRoom.Connections:GetChildren()) do
		
		local randomRoom = RarityFunction(workspace.GasPipeRooms)
		local newRoom = randomRoom:Clone()
		local SelectedPoints = newRoom.Connections:GetChildren()
		local points = PrevRoom.Connections:GetChildren()
		local SelectedPoint = points[math.random(1, #points)]
		
		newRoom.PrimaryPart = SelectedPoint
		newRoom.Parent = game.Workspace.GeneratedRoomsFolder.GeneratedGasPipeRooms
		newRoom:PivotTo(SelectedPoint.CFrame)
		SelectedPoint:Destroy()
		
	end
	
end

local PrevRoom = game.Workspace.GeneratedConcreteTunnels.Connector:WaitForChild("RightGeneration")

for i=1,50 do
	
	GenerateRoom(PrevRoom)
	
end
2 Likes

for the first index is there anything inside GasPipeRooms and is there anything inside Connections

3 Likes

everything is inside its respective folders, and there are models inside GasPipeRooms

so your saying the model is not appearing anywhere

1 Like

it doesn’t load anywhere, its supposed to clone and pivot to the respective connectionpoint and well it doesn’t do that

please add prints so you can know at which part of the code it stops, also try using this:

local roomsLimit = 100
local GenerateDelay = true
local RoomsGenerating = 0
local CurrentNum = 1

-- Setups
local MaximumRooms = 100
local MinimumRooms = 1

local function RarityFunction(Rooms)
    local Weight = 0
    for _, Room in pairs(Rooms:GetChildren()) do
        Weight += Room.Rarity.Value
    end
    
    local randomWeight = Random.new():NextInteger(1, Weight)
    local currentWeight = 0
    local randomRoom = nil
    for i, Room in pairs(Rooms:GetChildren()) do
        currentWeight += Room.Rarity.Value
        if randomWeight <= currentWeight then
            randomRoom = Room
            break
        end
    end
    return randomRoom
end

local function GenerateRoom(PrevRoom)
    if RoomsGenerating >= roomsLimit then
        return
    end
    
    for i, v in pairs(PrevRoom.Connections:GetChildren()) do
        local randomRoom = RarityFunction(workspace.GasPipeRooms)
        if randomRoom then
            local newRoom = randomRoom:Clone()
            local SelectedPoints = newRoom.Connections:GetChildren()
            local points = PrevRoom.Connections:GetChildren()
            local SelectedPoint = points[math.random(1, #points)]
            
            newRoom.PrimaryPart = SelectedPoint
            newRoom.Parent = game.Workspace.GeneratedRoomsFolder.GeneratedGasPipeRooms
            newRoom:PivotTo(SelectedPoint.CFrame)
            SelectedPoint:Destroy()
            
            RoomsGenerating += 1
            if RoomsGenerating >= roomsLimit then
                return
            end
        end
    end
end

local PrevRoom = game.Workspace.GeneratedConcreteTunnels.Connector:WaitForChild("RightGeneration")

for i = 1, 50 do
    GenerateRoom(PrevRoom)
end
1 Like
local roomsLimit = 100
local GenerateDelay = true
local RoomsGenerating = 0
local CurrentNum = 1

-- Setups
local MaximumRooms = 100
local MinimumRooms = 1

local function RarityFunction(Rooms)
	local Weight = 0
	for _, Room in pairs(Rooms:GetChildren()) do
		Weight += Room.Rarity.Value
	end

	local randomWeight = Random.new():NextInteger(1, Weight)
	local currentWeight = 0
	local randomRoom = nil
	for i, Room in pairs(Rooms:GetChildren()) do
		currentWeight += Room.Rarity.Value
		if randomWeight <= currentWeight then
			randomRoom = Room
			break
		end
	end
	return randomRoom
end

local function GenerateRoom(PrevRoom)
	print("1")
	if RoomsGenerating >= roomsLimit then
		return
	end
	
	print("2")

	for i, v in pairs(PrevRoom.Connections:GetChildren()) do
		print("3")
		local randomRoom = RarityFunction(workspace.GasPipeRooms)
		print("4")
		if randomRoom then
			print("5")
			local newRoom = randomRoom:Clone()
			print("6")
			local SelectedPoints = newRoom.Connections:GetChildren()
			print("7")
			local points = PrevRoom.Connections:GetChildren()
			print("8")
			local SelectedPoint = points[math.random(1, #points)]
			print("9")

			newRoom.PrimaryPart = SelectedPoint
			print("10")
			newRoom.Parent = game.Workspace.GeneratedRoomsFolder.GeneratedGasPipeRooms
			print("11")
			newRoom:PivotTo(SelectedPoint.CFrame)
			print("12")
			SelectedPoint:Destroy()
			print("13")

			RoomsGenerating += 1
			print("14")
			if RoomsGenerating >= roomsLimit then
				print("15")
				return
			end
		end
	end
end

print("16")

local PrevRoom = game.Workspace.GeneratedConcreteTunnels.Connector:WaitForChild("RightGeneration")
print("17")

for i = 1, 50 do
	GenerateRoom(PrevRoom)
	print("18")
end

only “16” prints

1 Like

this line holds the problem, its probably infinite yielding it because it doesnt exist

1 Like

what about the loop does it print nothing for the loop?

1 Like

Doesn’t print anything sadly cause the only number that prints is 16

It should exist because this script is activated once this part is generated

remove the part ItsRealJr said an see if it prints 17

1 Like

I changed the PrevRoom to a already existing part and this is the output:

And here is the script (again)

local roomsLimit = 100
local GenerateDelay = true
local RoomsGenerating = 0
local CurrentNum = 1

-- Setups
local MaximumRooms = 100
local MinimumRooms = 1

local function RarityFunction(Rooms)
	local Weight = 0
	for _, Room in pairs(Rooms:GetChildren()) do
		Weight += Room.Rarity.Value
	end

	local randomWeight = Random.new():NextInteger(1, Weight)
	local currentWeight = 0
	local randomRoom = nil
	for i, Room in pairs(Rooms:GetChildren()) do
		currentWeight += Room.Rarity.Value
		if randomWeight <= currentWeight then
			randomRoom = Room
			break
		end
	end
	return randomRoom
end

local function GenerateRoom(PrevRoom)
	print("1")
	if RoomsGenerating >= roomsLimit then
		return
	end
	
	print("2")

	for i, v in pairs(PrevRoom.Connections:GetChildren()) do
		print("3")
		local randomRoom = RarityFunction(workspace.GasPipeRooms)
		print("4")
		if randomRoom then
			print("5")
			local newRoom = randomRoom:Clone()
			print("6")
			local SelectedPoints = newRoom.Connections:GetChildren()
			print("7")
			local points = PrevRoom.Connections:GetChildren()
			print("8")
			local SelectedPoint = points[math.random(1, #points)]
			print("9")

			newRoom.PrimaryPart = SelectedPoint
			print("10")
			newRoom.Parent = game.Workspace.GeneratedRoomsFolder.GeneratedGasPipeRooms
			print("11")
			newRoom:PivotTo(SelectedPoint.CFrame)
			print("12")
			SelectedPoint:Destroy()
			print("13")

			RoomsGenerating += 1
			print("14")
			if RoomsGenerating >= roomsLimit then
				print("15")
				return
			end
		end
	end
end

print("16")

local PrevRoom = game.Workspace.ConnectorRoom.Connector
print("17")

for i = 1, 50 do
	GenerateRoom(PrevRoom)
	print("18")
end

First of all, the room is nowhere to be found even tho the output says otherwise
Second, the output first prints 1 & 2 but after the first generation function is ran then 1 & 2 isn’t outputed but instead the function starts from 3 and so on.

Edit: 3 was maybe outputed because the if statement wasn’t correct and it reset, but then it jumps from 14 to 18, and no room is found anywhere

local points = PrevRoom.Connections:GetChildren()

local SelectedPoint = points[math.random(1, #points)]

SelectedPoint:Destroy()

and prev room is never changing

so the pairs loop will eventually stop working

as for it starting at 3 i really have no idea

2 Likes

I fixed it somehow but I can’t really explain how but it still doesn’t have the effect I imagined so I will keep updating this code

how did you fix it and whats going wrong

1 Like