Issues with lag in game

Hello. I am having issues with lag in my game. Particularly a certain map that I have. I checked the microprofiler and it doesn’t seem too good. I was wondering how I can fix this problem because when I play in studio and live game, it seems to lag sort of bad and it can be really irritating. I am unsure if this is just maybe my wifi or if it is related to the map/game.

Right now what I think the issues might be but are not certain to be is the crowd movements and the plates located in the center of the map.

For the crowd: I have 36 models grouped into one big model. Within those 36 models are 36 parts.


There is a script named “Distributor” that gathers up the individual part in each model and clones a script into them that allows the part to move. That script looks like so:

wait(math.random(math.random(0.2,0.6),math.random(4,8))) --Breathe.
local Part = script.Parent
local distance = math.random(10,45) --The distance to move along the axis

local startingPos = Part.Position

while true do

	Part.Position = startingPos --Just in case it messese up.

	--Move upward:
	for i = 1, distance  do -- i represents the index or current position of the loop, it starts at one and goes to whatever the times to run is, meaning it will run that many times
		wait(0) -- Wait for the designated speed time
		Part.CFrame = Part.CFrame + Vector3.new(0, 0.1, 0) -- Change the CFrame by 0.1 to allow for a smooth transition
	end 

	wait(math.random(0.2,0.8))

	--Move downward:
	for i = 1, distance  do -- i represents the index or current position of the loop, it starts at one and goes to whatever the times to run is, meaning it will run that many times
		wait(0) -- Wait for the designated speed time
		Part.CFrame = Part.CFrame - Vector3.new(0, 0.1, 0) -- Change the CFrame by 0.1 to allow for a smooth transition
	end 

	wait(math.random(0.2,0.8))
end

for the center plates: I have 5 models grouped into one big model. Within those 5 models are 113 models that contain some 4-6 parts each.


There is another script named “Distributor” that gathers up each primary part of the 113 models and clones a script into that. The script that is cloned has a TouchedEvent inside of it so that when the players touch the pad, the color will change.

When I test this in live game and studio, it seems to lag and never seems to be running smoothly as I would like it too. Could I possibly get some help to figure out why this is happening? Is it related to my part count? Should I cancel cloning a script into EACH part and instead just make it into 1 script for all? I am unsure how to go about this problem. I simply want to fix the lag issue here.

1 Like

Definitely not the wifi. That is a lot of parts, a lot of waits(), math.random() calls, for loops, and everything is happening very quickly. That is your problem.

You could start by replacing your for loops and wait()s with Tweens. If there is still some lag, you might want to try reducing the number of crowd parts and/or running them from the client.

2 Likes

It is definitely not your wifi. I would suggest handling it all in one script. And if I were you, I would make this client-sided (run on a local script) so that the server doesn’t have to process all of the parts.

local TweenService = game:GetService("TweenService")
local info = TweenInfo.new(1,Enum.EasingStyle.Sine,Enum.EasingDirection.Out)

for _, parts in pairs(workspace.FloatingPads:GetDescendants()) do
    if parts:IsA("BasePart") then
        local distance = math.random(.5,2)
        local originalCFrame = parts.CFrame
        TweenService:Create(parts,info,{CFrame=parts.CFrame+distance}):Play()
        TweenService:Create(parts,TweenInfo.new(1,Enum,EasingStyle.Sine,Enum.EasingDirection.Outm0,false,math.random(0.2,0.8)),{CFrame=originalCFrame}):Play()
    end
end)

2 Likes

Along with what Salinas23 said about using tweens, you could also possibly achieve the movement effect with better performance by using physics constraints. You might be able to use this: AlignPosition | Roblox Creator Documentation

Here might be a cool fix! Do these effects on the client-side only then randomize the number of ‘people’ in the crowd to move based on the clients FPS so the client would have a steady frame rate and it will still look as if the crowd is cheering.

How can I randomize the number of ‘people’ in the crowd to move based on the clients FPS?

I would have to do it myself but here are my thoughts. First get the client’s FPS and set a min number of the crowd to move max being the whole crowd then move random ‘people’ in the crowd at a time while keeping track of the ‘people’ in the crowd moving. You could say if FPS is this and number of crowd is that then move a person or leave it alone. If you want to go further then you could keep track of how much each ‘person’ in the crowd is effecting the client put it in a variable and base it off that.