I've been experimenting with my custom characters in my spare time for some weeks now, attempting to pinpoint the source of a heinous spike in lag. I've finally found that whenever the animations in my custom character are active, my framerate dips drastically. For example, when I begin walking and stop, causing the idle animation to play, my FPS will dip about 5 - 6 frames and Remain at that level. I'm thinking that perhaps the animations are being fired In rapid succession however I am unsure. Can anyone provide some insight and help me debug this? Thanks. The script that animates the model is below.
--UPDATE-- I've removed the animations that don't seem relevant to the problem, and added a debounce. The debounce seemed to correct the issue of the idle animation firing too many times, however if the player taps the w quickly enough, the frames will begin to dip again.
-- Ibethia is on the planet Tirren! local player = game:GetService('Players').LocalPlayer Debounce = true local already_ran = false local grounded = false --local idleAnimation = Instance.new("Animation") -- idleAnimation.AnimationId = "rbxassetid://356344446" --local Left = Instance.new("Animation") --Left.AnimationId = "rbxassetid://356869859" --local Right = Instance.new("Animation") --Right.AnimationId = "rbxassetid://326922088" --local Forward = Instance.new("Animation") --Forward.AnimationId = "rbxassetid://312976450" --local Backward = Instance.new("Animation") -- Backward.AnimationId = "rbxassetid://312976450" local Jump = Instance.new("Animation") Jump.AnimationId = "rbxassetid://408621791" local Walk = Instance.new("Animation") Walk.AnimationId = "rbxassetid://408620308" local BackWalk = Instance.new("Animation") BackWalk.AnimationId = "rbxassetid://408620598" local RelaxedIdle = Instance.new("Animation") RelaxedIdle.AnimationId = "rbxassetid://408621630" local Blink = Instance.new("Animation") Blink.AnimationId = "rbxassetid://408621866" local Sprint = Instance.new("Animation") Sprint.AnimationId = "rbxassetid://409050900" --repeat wait() until player.Character function playAnimation(parent) if Debounce == true then Debounce = false local AnimTrack = player.Character.Humanoid:LoadAnimation(parent) AnimTrack:Stop() wait(.01) AnimTrack:Play() wait(0.1) Debounce = true --AnimTrack:AdjustSpeed(1.5) end end function onGround(speed) if grounded == false then grounded = true end if speed > 1 and script.Asset.Value == ("Forward")then if already_ran == false then already_ran = true wait(0.1) playAnimation(Walk) end --elseif speed > 1 and script.Asset.Value == ("Backward")then --if already_ran == false then --already_ran = true --wait(0.1) --playAnimation(BackWalk) -- end --elseif speed > 1 and script.Asset.Value == ("Sprint")then --if already_ran == false then -- already_ran = true --player.Character.Humanoid.WalkSpeed = 16 ---- wait(0.1) -- playAnimation(Sprint) --end elseif speed < 1 then script.Asset.Value = ("Chillin") print("Chillin") already_ran = false --wait(0.1) playAnimation(RelaxedIdle) wait(0.5) end end function onKeyPress(inputObject, gameProcessedEvent) local D = script.Debounce if inputObject.KeyCode == Enum.KeyCode.A then print("Left!!") script.Asset.Value = ("Left") elseif inputObject.KeyCode == Enum.KeyCode.D then script.Asset.Value = ("Right") elseif inputObject.KeyCode == Enum.KeyCode.W then script.Asset.Value = ("Forward") elseif inputObject.KeyCode == Enum.KeyCode.S then script.Asset.Value = ("Backward") elseif inputObject.KeyCode == Enum.KeyCode.E then script.State.Value = ("Emotional") elseif inputObject.KeyCode == Enum.KeyCode.One and script.State.Value == ("Emotional") then playAnimation(Sprint) elseif inputObject.KeyCode == Enum.KeyCode.Space and D.Value == true then print("CantJump") elseif inputObject.KeyCode == Enum.KeyCode.Space and D.Value == false then D.Value = true playAnimation(Jump) wait(0.4) D.Value = false end end game:GetService("UserInputService").InputBegan:connect(onKeyPress) player.Character.Humanoid.Running:connect(onGround) while true do wait(math.random(2,5)) playAnimation(Blink) end
Best advice I can give is try and isolate exactly which animation(s) is causing the lag. And perhaps rename the animations from "Parent". By seeing exactly which animation is causing the lag, it may help find the cause of it in the code, but an initial lookthrough isn't bringing anything to my attention.