Random Room Gen LAG

Hi! I’m making a random room generator and it is really laggy. Here is my script:

local rooms = game.Workspace.MapParts:GetChildren()
local startingroomnum = math.random(1, #rooms)
local startingroom = rooms[startingroomnum]
local startpart = game.Workspace.RoomsStart
local amountToGen = 30

print(startingroom)
print("This is starting room ^^^")


local startcopy = startingroom:Clone()
startcopy.Name = "startroom"
startcopy.Parent = workspace
startcopy.PrimaryPart = startcopy.floor

startcopy:PivotTo(startpart:GetPivot())


for i = 1, amountToGen do
	local currentroomnum = math.random(#rooms)
	local currentroom = rooms[currentroomnum]
	local currentclone = currentroom:Clone()
	currentclone.Name = "newroom"
	currentclone.PrimaryPart = currentclone.floor
	currentclone.Parent = workspace
	currentclone:PivotTo(startpart:GetPivot() + Vector3.new(0,0,76*i))
	print(currentroom)
	wait(0.05)
end

If anyone knows a way to make it less laggy, thanks so much :slight_smile:

1 Like

I suggest making the rooms no bigger than (20,15,20). I once made a backrooms game that generated about 1600 ‘rooms’. It didn’t lag until I made it generate one row at a time.

I see from your code that you made row-length cells and you’re generating it really quickly too.
So uh. Chop up the room a bit and yeah, you’ll be a bit better off I think.

local Size = 20 --Room must be square. Change this size to the length or width of the room.
local rooms = game.Workspace.MapParts:GetChildren()
local startingroomnum = math.random(1, #rooms)
local startingroom = rooms[startingroomnum]
local startpart = game.Workspace.RoomsStart
local amountToGen = 30

print(startingroom)
print("This is starting room ^^^")


local startcopy = startingroom:Clone()
startcopy.Name = "startroom"
startcopy.Parent = workspace
startcopy.PrimaryPart = startcopy.floor

startcopy:PivotTo(startpart:GetPivot())

for x = 1, amountToGen do --Added an extra loop so it generate in a square instead of a line.
for i = 1, amountToGen do
	local currentroomnum = math.random(#rooms)
	local currentroom = rooms[currentroomnum]
	local currentclone = currentroom:Clone()
	currentclone.Name = "newroom"
	currentclone.PrimaryPart = currentclone.floor
	currentclone.Parent = workspace
	currentclone:PivotTo(startpart:GetPivot() + Vector3.new(Size*x,0,Size*i))
	print(currentroom)
	task.wait(0.05)
end
task.wait(0.05
end
4 Likes

Okay. The length and width of the rooms is 76 studs.

This is what I am left with. I think it would be very inconvenient to scale down the rooms, but you helped me with another problem; making a square. Thanks for the wrong reason lol.

(Btw like this, it’s still laggy)

Edit: Okay. By removing the task.wait(0.05) It has certainly decreased the lag. After it’s generated, it’s still pretty laggy with the rooms just being there.

Oh yeah, with that complexity of a map.
For my game, each room had a sound and light but it wasn’t that laggy.
Try making as many things into a union as possible. This might help with shadow lighting lag.

First try disabling globalshadows in lighting to see if it helps.

1 Like

Shadows were already turned off. It would be kind of a pain to remove some stuff so idk what to do. There are 100% maps bigger than this so idk.

Im pretty sure most games remove rooms that are away from you. Amount of details should be lower, for example using meshes would reduce amount of faces/edges if you havent yet, streaming enabled obviously.

1 Like

I have streaming enabled. I can’t remove rooms as players are far from them because multiple players play this at the same time in very different places all around the map.

Is there a way I can reduce the render distance or something? I am looking for tutorials but they are all outdated.

You could loop through all the rooms via the client and set the part’s transparencies inside then to 1 if that’s not the room the player is in.

And also turn of collisions because I’m pretty sure PhysicsService takes up a bunch of memory too.

1 Like

Is it okay if you possibly show me how to do this. sry (I kinda need to be able to collide with stuff tho.)

Alright sure, it may not work cuz I’m typing this on my phone.
Also this should go under the “amountgen” pairs loops:

for i, room in pairs(rooms:GetChildren()) do
if room ~= rooms[currentroomnum] then
for i, part in pairs(room) do
if part:IsA(“Part”) then
part.Transparency = 1
part.CanCollide = false
else
-- Nothing here unless you wanna do smthn
end
end
end
end

This is the “amountgen” loops I’m mentioning:

1 Like

Wait, should this be in a local script? Because the room generator is a server script.

Yeah it should be in a local script because if not, everyone in the rooms behind would see nothing. And it wouldn’t work anyways because this is designed to find a specific player’s current room and get rid of the rest (if there were multiple player’s, then all the rooms would be gone even if it worked.

What you should do is fire a remoteEvent to the client and paste my code in there. And then lemme know if it works or not.

1 Like

Just use streamingenabled lol.
It automatically sets the maximum view distance of parts and reduced lag.

Workspace>StreamingEnabled
StreamingEnabled>StreamingMin = 200
StreamingEnabled>StreamingTarg = 400

Suggest those settings for the best gameplay.
Also with a localscript, there might be certain issues with exploits.

2 Likes

That really helped. Thanks

@Doomcolp Also thanks to you for helping so much!

1 Like

Actually @Redluo is correct. StreamingEnabled doesn’t get rid of rooms, it just doesn’t render them (if the player isn’t close enough) for a specific player. Everyone else who is closer to those room will see them.

1 Like

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