Hey! This script is a random room generator. It is REALLY laggy when I run it. Can anyone find the issue? Thanks
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
(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
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)
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
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!
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.
There are a few issues that could be causing lag in this script:
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.
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.
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.
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.
There are a few things that you can do to try to improve the performance of this script.
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.
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.
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.
You can try to optimize the script by minimizing the number of calls to the Random object and the Rooms collection.
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.
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.