How do You Find How Long RunService.Stepped Has Been Running For Since Activated?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I wanted to run a RunService.Stepped Event that would readjust my CurrentCamera’s CFrame (As the CurrentCamera maintains the CFrames of a part while that part is welded to an NPC’s head) while an animation is playing and then disconnect that event when the animation was done playing.

  1. What is the issue? Include screenshots / videos if possible!

My camera script worked, but the RunService.Stepped continued to run after it was supposed to be disconnected. I realized this was because the first parameter for RunService.Stepped was calculating the total time I have been playing the game, instead of how long the RunService.Stepped was running. (The first parameter is a variable called “time” that “returns The duration (in seconds) that RunService has been running for”)

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I decided to try and replicate my problem via new baseplate and this is basically the problem

local RS = game:GetService("RunService")
local RunFunct

wait(6)

RunFunct = RS.Stepped:Connect(function(time,deltatime)
	print(time)
	
	if time>2 then
		RunFunct:Disconnect()
	end
end)

the output should print how long you have stayed in the game once, since the event is already disconnected due to surpassing the alluded time. What I want it to do is start running at 0 until it has ran for more than 2 seconds.

Then, I thought that maybe I could reset this timer somehow by adding time = 0 below the print:

local RS = game:GetService("RunService")
local RunFunct

wait(6)

RunFunct = RS.Stepped:Connect(function(time,deltatime)
	print(time)
	time = 0 <----------

	if time>2 then
		RunFunct:Disconnect()
	end
end)

This doesn’t do anything, probably because studio treats time as a separate variable to the parameter.

While making this topic, I looked into the description of the time parameter and notice how it says “returns The duration (in seconds) that RunService has been running for”
I thought to myself that maybe it’s because my RunService variable in this example wasn’t nested inside a function and instead defined during the beginning of the game, thus already accounting for time in the beginning of the game. This doesn’t mean anything however, because in the game that I was working on, my RunService was defined inside a function that triggers from a tap spacebar ProximityPrompt, yet it has the same issue.

RunService function in my actual game with the same problem:

function CastAnim(animDummy,animation,CurrentCam)
	local originalPosition = CurrentCam.CFrame
	local Anim = animDummy.Humanoid:LoadAnimation(animation)
	Anim.Priority = Enum.AnimationPriority.Action
	Anim:Play()
	
	local RS = game:GetService("RunService") <------ RunService variable in the function
	local StopRender = false
	MoveCamera = RS.Stepped:Connect(function(DurationOfStepped) <------Where the same problem occurs
		DurationOfStepped = 0
		print(DurationOfStepped)
		local CurrentCamera = game.Workspace.CurrentCamera

		CurrentCamera.CFrame = CurrentCam.CFrame
		if DurationOfStepped > Anim.Length then <----------- "Anim" is AnimationTrack
			print("should stop")
			StopRender = true
		end
	end)
	if StopRender == true then
		MoveCamera:Disconnect()
	end
end

TL;DR RunService.Stepped is getting total playtime instead of total time it has been running

I think I didn’t get it clearly, but you’re trying to disconnect a RunService.Stepped connection once an animation ends?

If that’s the case, what I’d try is wrapping the function’s contents inside a while loop that repeats itself until AnimationTrack.IsPlaying is false. Like so:

-- Based off of your last sample given
-- Don't forget that, due to this making use of a while loop, it'll yield the thread!
while Anim.IsPlaying then -- Equivalent to (Anim.IsPlaying ~= nil and Anim.IsPlaying == true)
    local CurrentCamera = game.Workspace.CurrentCamera

	CurrentCamera.CFrame = CurrentCam.CFrame
    RS.RenderStepped:Wait()
end

Or all of this could be scrapped if your intent was different.

Thank you for the solution! I haven’t used while loops in a super long time so I never thought of it that way. Everything is working now. :smile:

1 Like