How do I connect/disconnect a runservice loop in the loop?

Hello.

I was wondering how to connect or disconnect a RunService loop within the loop itself. Like this;

game:GetService("RunService").RenderStepped:Connect(function()
   if 1 + 1 == 2 then -- Checking if the loop should still run. (Example)
      print("Hey! This loop is still supposed to run!")
   elseif 1 + 1 == 3 then -- Checking if the loop should be stopped. (Example)
      print("Hey! This loop should be stopped!")
      break -- Just an example to give an idea.
   end
end)

I’ve thought about making a variable for the connection, like this;

local LoopConnection

LoopConnection = game:GetService("RunService").RenderStepped:Connect(function()
	if 1 + 1 == 2 then
		print("Hey! This loop is still supposed to run!")
	elseif 1 + 1 == 3 then
		print("Hey! This loop should be stopped!")
		LoopConnection:Disconnect()
	end
end)

But I cannot use this as I need to make multiple same loops for each player without variables.

1 Like
local connection 
connection = game:GetService("RunService").RenderStepped:Connect(function(delta)
     connection:Disconnect()
end)
2 Likes

Yeah I pointed out in the post that I can’t use variables to break the loop. I would, but in this particular situation I can’t. Is there not any other way to?

That’s the way you disconnect RBXScriptConnections, if you want it to disconnect based on a value the value would have to be outside the scope of the renderstepped function otherwise it will create a new one every renderstepped and always be equal to what you initially set it as.

This will never disconnect because 1 + 1 always equals 2 and never equals 3

Yeah, I was just using that as an example. Let me try and be more clear.

local LoopConnection

LoopConnection = game:GetService("RunService").RenderStepped:Connect(function()
	if game.Players.LocalPlayer.Character.Head then
		print("Hey! This loop is still supposed to run!")
	elseif not game.Players.LocalPlayer.Character.Head then
		print("Hey! This loop should be stopped!")
		LoopConnection:Disconnect()
	end
end)

That code would just error when there is no head, you need to use FindFirstChild()

local LoopConnection

LoopConnection = game:GetService("RunService").RenderStepped:Connect(function()
	if game.Players.LocalPlayer.Character:FindFirstChild("Head") then
		print("Hey! This loop is still supposed to run!")
	else
		print("Hey! This loop should be stopped!")
		LoopConnection:Disconnect()
	end
end)

Also I hope you are not using it to check if there’s a head. That is inefficient use events.

No, I’m using the loop to make a halo follow a player. The only thing is I can’t have the loop run if the player leaves the game.

renderstepped is local you put it in a local script, if the player leaves the game all local scripts stop running

I’m indeed handling it on the client, but I’m handling it for everyone on the client. Instead of server-replication, I’m making it client-replicated.

That means that the script won’t get deleted if the player leaves.

I’m not sure what you mean, all local scripts stop running when a player leaves.

You should never have the client replicate to other clients, that can and will be completely exploited

1 Like

Why use RenderStepped to make a halo follow a player? You could weld. If a weld is too “static” for you, and doesn’t behave in a fluid manner, you can use BodyMovers, but not the legacy ones. If I were you, I would reconsider using RenderStepped and consider AlignPosition and AlignOrientation.

4 Likes

The only thing an exploiter would be able to do is stop the loops by stopping the localscript on their client or make the loops faster by duplicating the localscripts. That’s it.

Okay I suggest you learn how the client server model works

It’s important to know that RenderStepped is not a loop - yielding will not prevent it from firing again.

You can create your halo on the server and give network ownership of it to the player wearing it, then in your RenderStepped function, if the head is there position the halo, otherwise position it elsewhere. Have the halo destroyed on PlayerRemoving, there is no need to disconnect your RenderStepped function.

RenderStepped:Connect(function()
    if character:FindFirstChild(‘Head’) then
        -- position halo above head
    else
        -- position / parent halo elsewhere temporarily until head is valid 
    end
end)
2 Likes

The answer seems irrelevant, does anybody know how to actually disconnect RunService inside the loop?

using a variable like the first guy said???