Hello! I was having trouble with a script. I fixed it, but at a great cost. It lags me to DEATH.
local room = {}
room.random = Random.new()
local roomtext = Instance.new("SurfaceGui")
local texttext = Instance.new("TextLabel")
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,50)
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 then
local overEntrance = Instance.new("Part")
overEntrance.CFrame = newRoom.Entrance.CFrame
overEntrance.Parent = newRoom.Entrance
overEntrance.Anchored = true
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)
overEntrance.Touched:Connect(function(hit)
local player = Players:GetPlayerFromCharacter(hit.Parent)
if player then
player.Character.PrimaryPart.CFrame = workspace.TPto.CFrame
end
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 can confirm that it is in fact the script that’s making it lag because I temporarily disabled it and it stopped lagging.
I believe that this is the bit making it lag:
print(run)
if run == 1 then
local overEntrance = Instance.new("Part")
overEntrance.CFrame = newRoom.Entrance.CFrame
overEntrance.Parent = newRoom.Entrance
overEntrance.Anchored = true
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)
overEntrance.Touched:Connect(function(hit)
local player = Players:GetPlayerFromCharacter(hit.Parent)
if player then
player.Character.PrimaryPart.CFrame = workspace.TPto.CFrame
end
end)
end)
end
newtext.Parent = prevRoom.Exit
newRoom.Parent = workspace.GeneratedRooms
roomnumber = roomnumber + 1
newtext.TextLabel.Text = roomnumber - 1
I don’t know why it’s lagging but any help would be appreciated.
As @PeZsmistic already said, putting a touched inside of a function already bound to another touched event well generate function calls literally exponentially by the second.
Also it owuld help if you told us where room.Generate() is used
There are a few potential reasons why this script might be causing lag in your game:
The room.Generate() function is called very frequently, possibly on every frame. This can cause the script to consume a lot of resources and cause lag in the game. To fix this, you can consider limiting the frequency at which the function is called, for example by using a timer or a debounce function.
The room.Generate() function clones and creates new instances of randomRoom and roomtext on every call. These operations can be expensive and cause lag in the game, especially if they are done frequently. To fix this, you can consider creating a pool of rooms and room texts that can be recycled and reused instead of creating new instances every time.
The room.Generate() function performs a random check on every call to see if it should create a new part. This can also contribute to the lag in the game. To fix this, you can consider moving the random check outside of the room.Generate() function, and only create the part if the random check passes.
The overEntrance.Touched event is connected to a function on every call to room.Generate() , which can also cause lag in the game. To fix this, you can consider connecting the event only once, outside of the room.Generate() function.
here is a modified version of your script that addresses the performance issues I mentioned above:
local Players = game:GetService("Players")
local room = {}
room.random = Random.new()
-- Create a pool of room texts that can be recycled
local roomTextPool = {}
local function getRoomText()
if #roomTextPool > 0 then
return table.remove(roomTextPool)
else
local roomtext = Instance.new("SurfaceGui")
local texttext = Instance.new("TextLabel")
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")
return roomtext
end
end
-- Create a pool of room parts that can be recycled
local roomPool = {}
local function getRoom()
if #roomPool > 0 then
return table.remove(roomPool)
else
local possibleRooms = workspace.Rooms:GetChildren()
local randomRoom = possibleRooms[room.random:NextInteger(1, #possibleRooms)]
return randomRoom:Clone()
end
end
local function returnRoom(room)
room.Parent = nil
table.insert(roomPool, room)
end
local function returnRoomText(roomText)
roomText.Parent = nil
table.insert(roomTextPool, roomText)
end
local roomnumber = 1
function room.Generate(prevRoom)
local newRoom = getRoom()
newRoom.PrimaryPart = newRoom.Entrance
newRoom:PivotTo(prevRoom.Exit.CFrame)
-- Only perform the random check once, outside of the room.Generate() function
if math.random(1,50) == 1 then
local overEntrance = Instance.new("Part")
overEntrance.CFrame = newRoom.Entrance.CFrame