You can write your topic however you want, but you need to answer these questions:
- 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.
- 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”)
- 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