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.
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.
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.
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.
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)