I have an animation of the player holding down a button, and then releasing the button and returning to a regular pose (All in one animation). I’m trying to make it so when the animation reaches a marker (exactly when the hand is pressing the button), it activates all the code for it being pressed (playing sound, pausing the animation to hold the position over the button, etc). However, if the player is lagging, it’s quite inconsistent, like the animation sometimes doesn’t pause in time, leading the hand to be above the button. Even worse, sometimes while you’re still holding down the button, you can turn and look around (you shouldn’t be able to until done with the animation but for some reason the lag lets the player do it anyway).
Update: I updated part of the code since loading an animation to humanoid is apparently deprecated, which I did not know. This still didn’t fix the issue of the hand not stopping consistently on the button during lag. (Though, I haven’t been able to see the bug where I can move my camera and stuff like I was able to do before, but that could very well be coincidence and it may still happen)
Here’s the part I updated:
local animatorPlayer = game.Workspace:WaitForChild("Folderstuff"):WaitForChild("PlayerAnims"):WaitForChild("PlayerSwitchPress")
local animTrackSwitch = humanoid:LoadAnimation(animatorPlayer)
Here’s what it’s updated to:
local PlayerAnimator = Instance.new("Animator")
PlayerAnimator.Parent = humanoid
local animatorPlayer = Folderstuff:WaitForChild("PlayerAnims"):WaitForChild("PlayerSwitchPress")
local animTrackSwitch = PlayerAnimator:LoadAnimation(animatorPlayer)
Update Update: The issue where I can move my camera when I’m not supposed to be able to still happens. This also means the player can turn away from the proximity prompt to end the button press. In other words, switching to animator didn’t fix the issue. I still have no idea why this is happening and I need help to find a more reliable way, whether I’m doing “GetMarkerReachedSignal” wrong or if there’s just a better and more reliable alternative to do this, anything helps.
Here’s an example of what I mean for moving the camera away while still holding down the proximity prompt (the selected bricks were created to test FPS lag):
i usually never trust GetMarkerReachedSignal to fire for important things, if you know how long it is until the animation event fires (or can find how long it is), then you could have a backup timer just incase – or skip the GetMarkerReachedSignal altogether
heres a function to do the backup timer (written w/ partial tests)
-- GetMarkerReachedSignal w/ a backup timer || yields
function SafeGetMarkerReachedSignal(anim:AnimationTrack, marker:string, backupTime:number)
-- using `BindableEvent:Wait` because I don't like how coroutines can mess with error tracing
local Event = Instance.new("BindableEvent")
local Connection; Connection = anim:GetMarkerReachedSignal(marker):Once(function()
Event:Fire()
end)
local thread = task.delay(backupTime, function()
Event:Fire()
end)
-- the yielding part
Event.Event:Wait() -- (*edited, this part was wrong)
-- cleanup
Event:Destroy()
Connection:Disconnect()
task.cancel(thread)
return
end
So what if I need a backup timer anyway, what’s the point of GetMarkerReachedSignal? Should I just not use it? I mean I tried to remove it and replace it with a task.wait of the exact time, which worked for regular stuff but during lag, this time the hand freezes BEFORE the hand even reaches the button.
I just want to know what the standard for doing this stuff is, like the best and most reliable way to do this because I’m going to be doing this a lot for what I’m making
I don’t think there’s a standard for animation signals (im sure someone will correct me if there is), you’re gonna have to experiment and find something that works for you – I only use GetMarkerReachedSignal for things like VFX (footsteps, particle emission) that I’m okay with potentially not happening
You can try adding 0.1s (or some other short time) to the backupTime in that SafeGetMarkerReachedSignal function to hopefully get the best of both worlds
imo, if a player’s lagging badly enough, then they have bigger issues than some effect not lining up with an animation
… additionally, if you reaaally care about your animations, I like using this custom animation module, but it might require some refactoring
If there really is no standard for that, how do so many games on Roblox with lots of animations keep code synced with their animations almost perfectly, without the issue I’m having?
Also I do need to find SOME fix because it’s more then just an effect, it’s necessary for the game and I don’t want to accidentally make it so laggy players have advantages/disadvantages.
I just want to know how most games on Roblox achieve that effect.
(Sorry for the somewhat late response I’ve been sick)
That misunderstanding of how threads work made the same problem significantly worse.
You overengineered a simple watchdog thread.
local function YieldForMarker(anim:AnimationTrack, marker:string, TimeOut:number):()
task.delay(TimeOut,coroutine.running())
anim:GetMarkerReachedSignal(marker):Wait()
end
Don’t make it a function though; it’s a dumb performance waste having 2 lines as a function.
I reckon there’s probably a few causes for the bugs
the overshooting is probably just slight variations in frame rate — try setting the press animations “TimePosition” property after the event is reached (to whatever time the animation is supposed to be at) and then pausing it
Something like:
-- ...
animTrackSwitch:GetMarkerReachedSignal("ButtonPress"):Wait()
animTrackSwitch.TimePosition = 5.0 -- you would change this time to whatever
counting = true
animTrackSwitch:AdjustSpeed(0)
-- ...rest of code
the camera moving around though, I’m not sure, maybe it’s just from not having a debounce. try debugging with prints to see if things aren’t happening in the expected order
animation evnts are only unreliable when you start lagging mid animation , other than that its pretty consistent ( for me personaly )
i made a module that listens for events and such and it works great for me
you could make a module that reads all the animation events in an animation, and then it returns the wait time for each anim event( so you can use task.delay or task.wait instead of anim events )
In case it is an error of my own, I’ve decided to record a video rather then just using screenshots (Recorded in Roblox studio, using a bunch of unanchored parts to simulate lag):
Without lag, working (basically) as expected:
With lag (Ignore the error sound effects), notice how if I move my mouse around it lets me stop looking at the proximity prompt and cancel it early (without lag I cannot do that until the animation is finished, just as I want):
Ok so after looking over my code again and again and reviewing this, I think the issue of the camera moving lies with the “CameraAnimation” stuff. If I remember correctly, this code was from someone else and I edited it to add pausing and starting, but it seems to have caused the issue. The code’s goal is to allow me to animate the camera along with the player inside of moon animator, and play both the camera and player animation simultaneously. (Right now there’s no movement from the animation to the camera but I intended to add some). Here’s the code:
local CamAnimator = {}
local RunService = game:GetService("RunService")
local Character = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
local Camera = workspace.Camera
CamAnimator.Paused = false
function CamAnimator.Pause()
CamAnimator.Paused = true
end
function CamAnimator.Resume()
CamAnimator.Paused = false
end
function CamAnimator.Cinematic(path)
local CinematicsFolder = game.Workspace.Folderstuff.CameraAnimations[path] -- Path to your folder!
local CurrentCameraCFrame = workspace.CurrentCamera.CFrame
local FrameTime = 0
local Connection
Character.Humanoid.AutoRotate = false
Camera.CameraType = Enum.CameraType.Scriptable
Connection = RunService.RenderStepped:Connect(function(DT)
if CamAnimator.Paused then
return
end
FrameTime += (DT * 60)
-- This will convert the seconds passed (DT) to the frame of the camera we need.
-- Then it adds it to the total amount of time passed since the animation started
local NeededFrame = CinematicsFolder.Frames:FindFirstChild(tonumber(math.ceil(FrameTime)))
if NeededFrame then
Camera.CFrame = NeededFrame.Value
else
Connection:Disconnect()
Character.Humanoid.AutoRotate = true
Camera.CameraType = Enum.CameraType.Custom
Camera.CFrame = CurrentCameraCFrame
end
end)
end
return CamAnimator
As you can see, I forgot to make it so CamAnimator.Paused actually DOES anything (probably because I couldn’t figure out how or something). So at least I know WHY, though I still need to figure out how to make the camera position and rotation to pause on time of course…
Of course, this doesn’t fix the issue of the animation marker not activating quick enough (or activating too early) to stop the hand perfectly (It’s more noticeable with lag but I’ve realized even without lag it does a bit too), but unless someone has a concrete fix for that as well, I’ll probably just set the time position to the marker exactly right before the pause (though that causes a bit of jitter, it’s only noticeable if you’re looking out for it)
Yeah it is, I noticed the same in my game as well. When the server is under a lot of stress, the signal might be skipped entirely (or maybe delayed?)
My code is very straight forward and it would never exit the loop or take a VERY long time. Neither Stopped or Marker Signal would trigger
local ReloadCompleteConnection = ReloadAnimation:GetMarkerReachedSignal("ReloadComplete"):Once(function()
ReloadDone = true
end)
local StoppedConnection = ReloadAnimation.Stopped:Once(function()
ReloadStopped = true
end)
repeat
task.wait(LOOP_TIME)
until ReloadDone or BreakReload or ReloadStopped
Loaded = not BreakReload and ReloadDone
You should likely open a proper bug report so Roblox can investigate