"RunService.RenderStepped:Connect(function()" Causing to low fps

Hi! I’m doing a system for my game. The system is:
There’s a bass hit animations like: Dab
and this animations playing to Music/Sound bass and it uses Playbackloudness for doing bass hit animation. So i wanted to make animations play/stop smoothier and i used “RunService.RenderStepped:Connect(function()” everything is okay but after a while, script causing to low FPS

Here script:

-For Function

function BassHit()
	RunService.RenderStepped:Connect(function()
		if LocalPlayer:FindFirstChild("PlayerFolder") and LocalPlayer.PlayerFolder:FindFirstChild("SelectedBassHit") and LocalPlayer.PlayerFolder.SelectedBassHit.Value == "Dab" then
			Dab:Play()
			Dab:AdjustSpeed(script.TweenValue2.Value)
		Dab:AdjustWeight(script.TweenValue2.Value)
		BassHitt = true
		end

		if LocalPlayer:FindFirstChild("PlayerFolder") and LocalPlayer.PlayerFolder:FindFirstChild("SelectedBassHit") and LocalPlayer.PlayerFolder.SelectedBassHit.Value == "T-Pose" then
			TPose:Play()
			TPose:AdjustSpeed(script.TweenValue2.Value)
			TPose:AdjustWeight(script.TweenValue2.Value)
		end

		if LocalPlayer:FindFirstChild("PlayerFolder") and LocalPlayer.PlayerFolder:FindFirstChild("SelectedBassHit") and LocalPlayer.PlayerFolder.SelectedBassHit.Value == "Thrust" then
			Thrust:Play()
			Thrust:AdjustSpeed(script.TweenValue2.Value)
			Thrust:AdjustWeight(script.TweenValue2.Value)
		end
	end)
end

Starts function:

  • Also this one uses “RunService.RenderStepped:Connect(function()” too
if Track.PlaybackLoudness >= Track:WaitForChild("AmountValue").Value then
			local Tween = TweenService:Create(script.TweenValue2, TweenInfo.new(0.05, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false, 0), {Value = Track.PlaybackLoudness * 0.009}):Play()
			BassHit()
		elseif Track.PlaybackLoudness < Track:WaitForChild("AmountValue").Value then
			if Dab.IsPlaying == true then
				Dab:Stop()
				local Tween = TweenService:Create(script.TweenValue2, TweenInfo.new(0.05, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false, 0), {Value = 0}):Play()
			end

			if TPose.IsPlaying == true then
				TPose:Stop()
				local Tween = TweenService:Create(script.TweenValue2, TweenInfo.new(0.05, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false, 0), {Value = 0}):Play()
			end	

			if Thrust.IsPlaying == true then
				Thrust:Stop()
				local Tween = TweenService:Create(script.TweenValue2, TweenInfo.new(0.05, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false, 0), {Value = 0}):Play()
			end	
		end	

Please tell me how can i fix that.

Looks like an memory leak, you keep creating new .RenderStepped events with BassHit() without clearing the previous events.

How can i remove or end the old renderstepped event?

You could store it in a variable outside your code

local Event
function BassHit()
	if Event then Event:Disconnect() end -- Disconnect old event
	Event = RunService.RenderStepped:Connect(function()
        [...]

however I suggest getting rid of this function entirely and relying on just 1 .RenderStepped so you don’t need to keep creating new / removing old events every time.

Thank you so much! Now it’s working correctly, and it’s not causing to low FPS.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.