I have a .RenderStepped function that I disconnect in my code and I want to know if there is a way I can “Reconnect” the function. If there isn’t a way already built into roblox how would I reconnect it manually? If that’s a bit confusing I want something like this:
local RStepped = game:GetService("RunService").RenderStepped:Connect(function()
print("Hello")
end)
wait(1)
RStepped:Disconnect()
wait(3)
RStepped:Reconnect() -- Something like this?
No, but you can declare the function externally and reconnect it like that:
local function OnRenderStepped()
print("Hello")
end
local conn = game:GetService("RunService").RenderStepped:Connect(OnRenderStepped)
wait(1)
conn:Disconnect()
wait(3)
conn = game:GetService("RunService").RenderStepped:Connect(OnRenderStepped)
This would work, but I’m disconnecting it multiple times since I’m disconnecting the function whenever somebody reloads in my game. So If I were to do that it would only be able to be disconnected one time from what I’ve tested.
Sorry, should’ve explained it a little better so let me give you the run down. In my game I’m making I have a .RenderStepped function that updates a viewModel’s arms to the position of 2 parts on the weapon (I used this tutorial by the way). So to be able to play animations on the viewModel I need to disconnect the .RenderStepped function that updates the arms since this causes issues, then I want to reconnect the function. So kind of think it has this code shown below:
local OnUpdateArmsStepped = game:GetService("RunService").RenderStepped:Connect(UpdateArms)
-- This is defined before the bottom function
--Player press R key:Connect(function()
OnUpdateArmsStepped:Disconnect()
Animation:Play()
local OnUpdateArmsStepped = game:GetService("RunService").RenderStepped:Connect(UpdateArms) -- Reconnecting the function
end)
I can only reconnect it one time before it doesn’t disconnect again. (I’m really sorry if that wasn’t completely clear I’m in a rush right now )
Why not just use BindToRenderStep here? Seems like it would solve your use case with a nice, simple API. It will allow you to bind and unbind a function by name to be ran per render step. You don’t want to use the standard RenderStepped event if you keep connecting and disconnecting; and in the first place, you shouldn’t have a need to frequently do this regardless.
local function RenderStepCallback()
print("Hello")
end
local RunService = game:GetService("RunService")
local RenderStepped = RunService.RenderStepped
local Connection = RenderStepped:Connect(RenderStepCallback)
wait(1)
Connection:Disconnect()
wait(3)
Connection = RenderStepped:Connect(RenderStepCallback)
That tutorial is a bit outdated (e.g. it doesn’t use the TweenService for the aiming down sights animation)
That tutorial also doesn’t take into account animations on the view model at all – you’re going to need a few more changes than just “disconnect the render stepped” if you want it to look good. For example, what happens if you’re aiming down sights and you play your animation? Do you handle that case?
@colbert2677 is right in that BindToRenderStepped would be more appropriate, but I also don’t believe that’s the core issue here
You definitely can connect a function multiple times, as @ClockworkSquirrel and myself pointed out. I’m not sure what issue you’re referring to. You’re redefining OnUpdateArmsStepped twice in yours btw, remove the second local (that won’t change how your code works though).
You could just wrap your UpdateArms function in a big if not animating then statement or something