Script REALLY laggy

Hey! This script is a random room generator. It is REALLY laggy when I run it. Can anyone find the issue? Thanks :slight_smile:

local room = {}
room.random = Random.new()
local roomtext = Instance.new("SurfaceGui")
local texttext = Instance.new("TextLabel")
local runonce = false
texttext.Parent = roomtext
texttext.TextSize = 100
texttext.Size = UDim2.new(1,0,1,0)
texttext.BackgroundTransparency = 1
texttext.BorderSizePixel = 0
roomtext.Face = "Back"
texttext.Font = "Creepster"
texttext.TextColor = BrickColor.new("Grey")
local roomnumber = 1
function room.Generate(prevRoom)
	local run = math.random(1,1)
	local shadow = math.random(1,1)
	local possibleRooms = workspace.Rooms:GetChildren()
	local randomRoom = possibleRooms[room.random:NextInteger(1, #possibleRooms)]
	local newRoom = randomRoom:Clone()
	local newtext = roomtext:Clone()
	newRoom.PrimaryPart = newRoom.Entrance
	newRoom:PivotTo(prevRoom.Exit.CFrame)
	print(run)
	if run == 1 and runonce == false and roomnumber > 5 then
		runonce = true
		local overEntrance = Instance.new("Part")
		local TpawayFromRun = workspace.TpAwayRun
		overEntrance.CFrame = prevRoom.Exit.CFrame
		overEntrance.Size = Vector3.new(0.1,0.1,0.1)
		overEntrance.Parent = prevRoom.Exit
		overEntrance.Anchored = true
		overEntrance.Transparency = 1
		overEntrance.CanCollide = false
		overEntrance.CanTouch = true
		overEntrance.Size = Vector3.new(3,3,3)
		local Players = game:GetService("Players")
		overEntrance.Touched:Connect(function(part)
			local player = Players:GetPlayerFromCharacter(part.Parent)
				if player then
					player.Character.PrimaryPart.CFrame = workspace.TPto.CFrame
			end
		end)
		TpawayFromRun.Touched:Connect(function(part)
			local away = Instance.new("Part")
			away.Name = "awaylol"
			away.Anchored = true
			away.CanCollide = false
			away.CFrame = newRoom.Exit.CFrame
			local player = Players:GetPlayerFromCharacter(part.Parent)
			if player then
				player.Character.PrimaryPart.CFrame = away.CFrame
			end
		end)
		overEntrance.Touched:Connect(function(part)
			local player = Players:GetPlayerFromCharacter(part.Parent)
			if player then
				player.Character.PrimaryPart.CFrame = workspace.TPto.CFrame
			end
		end)
	end
	newtext.Parent = prevRoom.Exit
	newRoom.Parent = workspace.GeneratedRooms
	roomnumber = roomnumber + 1
	newtext.TextLabel.Text = roomnumber - 1
	return newRoom
end

return room
2 Likes

First off a few things:

  1. I don’t see why you got: math.random(1,1) you can just set it to: 1
  2. Are the rooms pre-loaded and how many parts do they have?
  3. Are you sure its not other scripts or assets that cause lag?
  4. What are your computer specs?
  1. That is for testing purposes and will be put up after done
  2. The rooms are pre loaded and they aren’t super massive
  3. I am sure. I disabled this script and the lag went away
Device name PredatorHelios
Processor 11th Gen Intel(R) Core™ i7-11800H @ 2.30GHz 2.30 GHz
Installed RAM 16.0 GB (15.8 GB usable)
Device ID 198062BD-04AE-41D3-8E44-F97E5DADDB02
Product ID 00342-20843-46317-AAOEM
System type 64-bit operating system, x64-based processor
Pen and touch No pen or touch input is available for this display

(I will edit this instead of sending new messages)

First off its best if you make most Instance.new() already pre-made

Change: local randomRoom = possibleRooms[room.random:NextInteger(1, #possibleRooms)] to: local randomRoom = possibleRooms[math.random(1, #possibleRooms)]

You only have to call: local possibleRooms = workspace.Rooms:GetChildren() once unless new rooms get added to that later

(Reply to me if this helps or not)

2 Likes

touched functions could lag out scripts a lot especially when its creating a part. try removing them and see if it is still lagging

3 Likes

I think what you suggested helped a bit but it’s still lagging. Thanks for the help though.

Yea true @DevHelpAccount its best if you add debounce to it

local Debounce = false

part.Touched:Connect(function(hit)
    if not Debounce then
      Debounce = true
       -- // Do Task
       -- You can add a wait() here
      Debounce = false
    end
end)
2 Likes

I temporarily turned all of the touched events into comments and the lag continued. (The lag happens as soon as I load in the game)

I think it might have something to do with the instance.new() but I don’t know how to fix that.

1 Like

Well you can pre-make those instances and then just :Clone() em in

local CoolPart = script.Part:Clone()

CoolPart.Parent = newroom
2 Likes

How many times do you call this function?
What is the script that actually uses this?

This by itself doesn’t seem to have anything that could lag the game.

I advise you disconnect the touch functions when the room is deleted (if it gets deleted).

1 Like

This is the new script. It still lags…
(Those instance.new at the the top don’t matter, right?)

local room = {}
room.random = Random.new()
local roomtext = Instance.new("SurfaceGui")
local texttext = Instance.new("TextLabel")
local runonce = false
texttext.Parent = roomtext
texttext.TextSize = 100
texttext.Size = UDim2.new(1,0,1,0)
texttext.BackgroundTransparency = 1
texttext.BorderSizePixel = 0
roomtext.Face = "Back"
texttext.Font = "Creepster"
texttext.TextColor = BrickColor.new("Grey")
local possibleRooms = workspace.Rooms:GetChildren()
local roomnumber = 1
function room.Generate(prevRoom)
	local run = math.random(1,1)
	local shadow = math.random(1,1)
	local randomRoom = possibleRooms[math.random(1, #possibleRooms)]
	local newRoom = randomRoom:Clone()
	local newtext = roomtext:Clone()
	newRoom.PrimaryPart = newRoom.Entrance
	newRoom:PivotTo(prevRoom.Exit.CFrame)
	print(run)
	if run == 1 and runonce == false and roomnumber > 5 then
		runonce = true
		local overEntrance1 = workspace.oe
		local overEntrance = overEntrance1:Clone()
		local TpawayFromRun = workspace.TpAwayRun
		overEntrance.CFrame = prevRoom.Exit.CFrame
		overEntrance.Size = Vector3.new(0.1,0.1,0.1)
		overEntrance.Parent = prevRoom.Exit
		overEntrance.Anchored = true
		overEntrance.Transparency = 1
		overEntrance.CanCollide = false
		overEntrance.CanTouch = true
		overEntrance.Size = Vector3.new(3,3,3)
		local Players = game:GetService("Players")
		overEntrance.Touched:Connect(function(part)
			local player = Players:GetPlayerFromCharacter(part.Parent)
				if player then
					player.Character.PrimaryPart.CFrame = workspace.TPto.CFrame
			end
		end)
		TpawayFromRun.Touched:Connect(function(part)
			local away1 = game.Workspace.away1
			local away = away1:Clone()
			away.Name = "awaylol"
			away.Anchored = true
			away.CanCollide = false
			away.CFrame = newRoom.Exit.CFrame
			local player = Players:GetPlayerFromCharacter(part.Parent)
			if player then
				player.Character.PrimaryPart.CFrame = away.CFrame
			end
		end)
		overEntrance.Touched:Connect(function(part)
			local player = Players:GetPlayerFromCharacter(part.Parent)
			if player then
				player.Character.PrimaryPart.CFrame = workspace.TPto.CFrame
			end
		end)
	end
	newtext.Parent = prevRoom.Exit
	newRoom.Parent = workspace.GeneratedRooms
	roomnumber = roomnumber + 1
	newtext.TextLabel.Text = roomnumber - 1
	return newRoom
end

return room

How can I make it get deleted after all players are in the next room? This may be causing lag

I’m guessing the prevRoom is the room model.
You should use that to take care of the previous room.

I’m guessing the prevRoom is the room model.
You should use that to take care of the previous room.

I also highly advise you store the Touched connections so you can disconnect them after the room is destroyed.

This probably won’t fix the lag because I doubt it would lag the game when it is initially run. It’s just a measure to prevent the rooms and connections stacking up and making the game lag even more over time.

If you’d tell us what are you trying to make It would be easier for us to understand what are you gonna achieve. Let’s just guess roblox “DOORS” type game. There is a great tutorial on youtube. He is pretty easy to understand. Goodluck with your game!

1 Like

Sorry for the really late reply. I did follow the first episode of this tutorial, room generation, but just kinda added stuff from there. Thanks for the help though. I think I fixed it anyway by adding a wait between each room generation.

Or you could make each room load by opening another door for example

Could we see the code where you’re calling room.Generate?

There are a few issues that could be causing lag in this script:

  1. Cloning: The script is cloning a large number of objects, which can be resource-intensive and cause lag. You can try reducing the number of objects being cloned or using instance caching to minimize the impact on performance.
  2. Function calls: The script is calling a number of functions repeatedly, which can also cause lag. You can try optimizing these functions to reduce the number of calls and improve performance.
  3. Random generation: The script is using the math.random() function to generate random numbers, which can be slow. You can try using a faster method of random number generation, such as the Random.new() method used in the script.
  4. Unnecessary calculations: The script is performing calculations that may not be necessary for the desired outcome. For example, the run and shadow variables are always set to 1 , regardless of the random number generated. You can try simplifying the script by removing unnecessary calculations.

By addressing these issues, you may be able to improve the performance of the script and reduce lag. It’s also a good idea to use a performance profiling tool to identify any other areas of the script that may be causing lag.

1 Like

There are a few things that you can do to try to improve the performance of this script.

  1. You can try reducing the number of objects that are being created and deleted. For example, the overEntrance object is being created and deleted every time the script is run. Instead, you could create the overEntrance object once and just reposition and enable/disable it as needed.
  2. You can try to reduce the number of times that the script is being run. One way to do this is by using a debounce function to limit the frequency at which the script is run.
  3. You can try to optimize the code by reducing the number of unnecessary calculations. For example, the run and shadow variables are being assigned a value of 1 every time the script is run, which is not necessary.
  4. You can try to optimize the script by minimizing the number of calls to the Random object and the Rooms collection.
  5. You can try to optimize the script by minimizing the number of calls to the PivotTo method. This method can be computationally expensive, especially if it is being called frequently.
  6. You can try to optimize the script by using more efficient data structures and algorithms. For example, instead of using a for loop to iterate over the children of the Rooms collection, you could use a while loop and break out of the loop once you have found the desired child.

I hope these suggestions help! Let me know if you have any questions.

1 Like