How do I break a RenderStepped Loop?

Haven’t really seen a good solution for this, can anyone help?

3 Likes

Try this:

local runService = game:GetService("RunService")
local event = runService.RenderStepped:Connect(function()
	print("YES")
end)

wait(5)
event:Disconnect()

That should answer your question.

1 Like

There’s the above, though have you additionally considered perhaps binding and unbinding from RenderStepped instead or does your use case absolutely need it to be a connection?

4 Likes

Well, to be more specific, here’s how I implemented it in my localscript:

local runService = game:GetService("RunService")

game.ReplicatedStorage.floorPuffs.OnClientEvent:Connect(function(plrName, state)
	
	for i,v in pairs(game:GetService("Players"):GetChildren()) do
		
		if v.Name == plrName then
			
			local player = v
			
			local part1 = game.Lighting.ClientVFX.FloorPuffs:Clone()
			part1.Parent = player.Character
			part1.Name = "floorPuff1"
			
			local connection
			
			if state == "Begin" then
				connection = runService.RenderStepped:Connect(function()
					part1.Position = player.Character["Left Leg"].Position + Vector3.new(0,-1.5,0)
				end)
			elseif state == "End" then
				if player.Character:FindFirstChild("floorPuff1") then
					player.Character.floorPuff1:Destroy()
				end
			end			
		end	
	end
end)

Is there any way to disconnect the renderstepped function when state == “End”?

I’ll give that a shot, forgot that existed

Yes, use connection:Disconnect(). Or use the bind the functions as colbert said.

saved me from a headache!

cheers,
-Expistic