I want to know the most effective way to disconnect a render stepped function
I have a camera shake module which contains code to shake the camera when the user
is running. they use the shift key to run. I use inputBegan and inputEnded which work fine but im not
too sure how i could disconnect the event. i could add a simple boolean value and an if statement but that wouldn’t be efficient as the renderstepped function is still running.
(there is a tween but that has nothing to do with my issue, that is another part of my code for something else)
local UIS = game:GetService("UserInputService")
UIS.InputBegan:Connect(function(input, processed)
if processed then return end
game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = norm_spd
tween2:Play()
if input.UserInputType == Enum.UserInputType.Keyboard then
if input.KeyCode == ke_y then
game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = speed
print("input started")
RunService.RenderStepped:Connect(BobbleModule.CameraShakeFunction)
tween:Play()
-- tween starts 1 and stops 2
end
end
end)
UIS.InputEnded:Connect(function(input)
if input.KeyCode == ke_y then
game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = norm_spd
print("input stopped")
-- how can i stop the renderstepped function here?
tween2:Play()
-- tween starts 2 and stops 1
end
end)
found the solution, assign the function as a variable.
--somewhere up here,
local connectfunc
game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = speed
print("input started")
connectfunc = RunService.RenderStepped:Connect(BobbleModule.CameraShakeFunction)
--where the input needs to end
UIS.InputEnded:Connect(function(input)
if input.KeyCode == ke_y then
game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = norm_spd
print("input stopped")
connectfunc:Disconnect() --disconnects the function
thanks for the help. I dont know why i didn’t see it sooner lol
As you’ve discovered there’s effectively only one way.
local Game = game
local RunService = Game:GetService("RunService")
local function OnRenderStep(Delta)
print(Delta)
end
local Connection = RunService.RenderStepped:Connect(OnRenderStep) --Assign the connection (RBXScriptConnection object) to a variable.
task.wait(1)
Connection:Disconnect() --Call 'Disconnect' to disconnect the connection.
Instead of storing a connection you can simply use RunService methods BindToRenderStepped and UnbindFromRenderStepped
Sample code
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local LocalPlayer = game.Players.LocalPlayer
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
UserInputService.InputBegan:Connect(function(Input, Processed)
if Processed then return end
LocalPlayer.Character.Humanoid.WalkSpeed = norm_spd
tween2:Play()
if Input.KeyCode == ke_y then
print("input started")
Humanoid.WalkSpeed = speed
RunService:BindToRenderStep("BobbleShake", 300, BobbleModule.CameraShakeFunction)
tween:Play()
end
end)
UserInputService.InputEnded:Connect(function(Input)
if Input.KeyCode == ke_y then
print("input stopped")
Humanoid.WalkSpeed = norm_spd
RunService:UnbindFromRenderStep("BobbleShake")
tween2:Play()
end
end)