Crucial FPS Drop -- How should I fix?

game:GetService("RunService").Heartbeat:Connect(function()
	local ray = Ray.new(root.Position, root.CFrame.UpVector* -500)
	local part = workspace:FindPartOnRayWithWhitelist(ray, {workspace.Spawn.SpawnNeon})
	if part then
		task.wait()
		if game.Workspace.Spawn.SpawnNeon ~= Color3.fromRGB(0, 255, 0) and Active == false then
			Active = true
			task.wait()
			local OldColor = game.Workspace.Spawn.SpawnNeon.Color
			local Value = game.Workspace.Spawn.SpawnColorVal
			Value.Value = OldColor
			local Beam1 = game.Workspace.Spawn.Lights.Beam1.Beam
			local Beam2 = game.Workspace.Spawn.Lights.Beam2.Beam
			local Beam3 = game.Workspace.Spawn.Lights.Beam3.Beam
			local Beam4 = game.Workspace.Spawn.Lights.Beam4.Beam

			local NewColor = Color3.fromRGB(0,255,0)


			local Tweena = game:GetService("TweenService"):Create(Value,TweenInfo.new(1,Enum.EasingStyle.Linear,Enum.EasingDirection.InOut,0,false,0),{Value = NewColor})
			local Tweenb = game:GetService("TweenService"):Create(Value,TweenInfo.new(1,Enum.EasingStyle.Linear,Enum.EasingDirection.InOut,0,false,0),{Value = NewColor})
			local Tweenc = game:GetService("TweenService"):Create(Value,TweenInfo.new(1,Enum.EasingStyle.Linear,Enum.EasingDirection.InOut,0,false,0),{Value = NewColor})
			local Tweend = game:GetService("TweenService"):Create(Value,TweenInfo.new(1,Enum.EasingStyle.Linear,Enum.EasingDirection.InOut,0,false,0),{Value = NewColor})
			Tweena:Play()
			Tweenb:Play()
			Tweenc:Play()
			Tweend:Play()

			Value.Changed:Connect(function(val)
				task.wait()
				Beam1.Color = ColorSequence.new(val)
				Beam2.Color = ColorSequence.new(val)
				Beam3.Color = ColorSequence.new(val)
				Beam4.Color = ColorSequence.new(val)
			end)

			local Info = TweenInfo.new(
				0.75, -- Time
				Enum.EasingStyle.Sine, -- Easing Style
				Enum.EasingDirection.Out, -- Easing Direction
				0, -- Repeats
				false, -- Reverse
				0 -- Delay
			)

			local ChangeColor =
				{
					Color = Color3.fromRGB(100, 255, 50),
				}

			local Tween1 = TweenService:Create(game.Workspace.Spawn:WaitForChild("SpawnNeon"), Info, ChangeColor)	

			Tween1:Play()
			task.wait()
			Active = false
		else
		end

This part of the script is causing a lot of lag, does anyone know why?
I just tried in-game and whenever this part of the code is running, ping increases and fps drops crucially.

6 Likes

It might get fixed if at the end of the heartbeat function you add the Line

game:GetService("RunService").RenderStepped:Wait()

i believe. Let me know if it works.

4 Likes

Yep, this has stopped the FPS drops.

Thanks for the help, I appreciate it.

1 Like

Nevermind…
After long periods off waiting while this function is active, FPS drops occur again and Avg. Ping increases drastically.

1 Like

Sorry, try this when using heartbeat loops:

RunService.Heartbeat:wait()

Maybe that will work

2 Likes

Or maybe consider adding a

task.wait()

I don’t really know how to use run service and stuff, but at least one of those should work

1 Like

Added a bunch of the task.wait and heartbeat:wait(), still no noticeable difference.

1 Like

Add a heartbeat:wait() at the end of the function, i made a test loop and it worked

1 Like
  1. you’re doing a lot of calculations (like raycast)

this is a memory leak
3. you’re tweening every heartbeat

1 Like

thats because his “loop” is not a loop, its a heartbeat connection, which executes every heartbeat or after the physics simulation iirc, and hes doing a lot of things in a single heartbeat (and a heartbeat:wait() or smth that yields it WILL NOT YIELD THE CONNECTION)

1 Like

How would I change this to be more efficient/fix these FPS drops?

1 Like

you could try using parallel luau actors (which is pretty bad because of its limitations but it’ll boost the fps when doing heavy calculations), change the frequency, and handle connections properly:

to

local connection = Value.Changed:Connect(...)

-- disconnect after use
connection:Disconnect()

(^ an example)

1 Like
	local connection = Value.Changed:Connect(function(val)
			task.wait()
			Beam1.Color = ColorSequence.new(val)
			Beam2.Color = ColorSequence.new(val)
			Beam3.Color = ColorSequence.new(val)
			Beam4.Color = ColorSequence.new(val)
		end)
		connection:Disconnect()

I changed it to this, but then it just broke the rest of the script. (The beams no longer change color)

1 Like

you need to disconnect it after the tween is finished (btw what are you trying to do?)

1 Like

I can show you in-game if you want, it’s sort of like a spawn area/safe zone and if your inside the beams are green and if your outside they are red, basically showing if you are safe. They transition colors in and out to look clean. (Well, should do)

1 Like

right, but why do you have so many tweens for seemingly the same parameters?

1 Like

There are 4 different beams, as the spawn area is a square.
So i just did a tween for each.

you only need one tween as you only have 1 “value” reference object, try implementing this pseudocode instead:

onValueChanged
 set all beam colors to the value's color as a color sequence

onHeartbeat
 is inside safe zone?
  is spawn zone already green and active is false?
   set active to true
   tween the value's color to the new color (green) 
   play the tween
   tween the safe zone color to red-green-ish color?
   play the tween
   wait until tween is finished (use tween.Completed:Wait())
   set active to false
  else (is outside)
   revert the changes

i moved the value changed connection to outside so that it doesnt memory leak and its supposed to only get connected once; you can also use a differnet method to check if the player is inside the safe zone (getpartsinpart, etc)

task.wait()
while task.wait() do
	local ray = Ray.new(root.Position, root.CFrame.UpVector* -500)
	local part = workspace:FindPartOnRayWithWhitelist(ray, {workspace.Spawn.SpawnNeon})
	if part then
		task.wait()
		if game.Workspace.Spawn.SpawnNeon ~= Color3.fromRGB(0, 255, 0) and Active == false then

Kinda just changed from heartbeat to while ? do, that works without lag/fps drops.

Thanks for the help.

1 Like