Why do these custom character animations lag? --Update--
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.
003 | local player = game:GetService( 'Players' ).LocalPlayer |
005 | local already_ran = false |
006 | local grounded = false |
017 | local Jump = Instance.new( "Animation" ) |
019 | local Walk = Instance.new( "Animation" ) |
021 | local BackWalk = Instance.new( "Animation" ) |
023 | local RelaxedIdle = Instance.new( "Animation" ) |
025 | local Blink = Instance.new( "Animation" ) |
027 | local Sprint = Instance.new( "Animation" ) |
034 | function playAnimation(parent) |
035 | if Debounce = = true then |
037 | local AnimTrack = player.Character.Humanoid:LoadAnimation(parent) |
049 | function onGround(speed) |
050 | if grounded = = false then |
053 | if speed > 1 and script.Asset.Value = = ( "Forward" ) then |
054 | if already_ran = = false then |
078 | elseif speed < 1 then |
080 | script.Asset.Value = ( "Chillin" ) |
084 | playAnimation(RelaxedIdle) |
095 | function onKeyPress(inputObject, gameProcessedEvent) |
096 | local D = script.Debounce |
097 | if inputObject.KeyCode = = Enum.KeyCode.A then |
099 | script.Asset.Value = ( "Left" ) |
100 | elseif inputObject.KeyCode = = Enum.KeyCode.D then |
101 | script.Asset.Value = ( "Right" ) |
102 | elseif inputObject.KeyCode = = Enum.KeyCode.W then |
103 | script.Asset.Value = ( "Forward" ) |
104 | elseif inputObject.KeyCode = = Enum.KeyCode.S then |
105 | script.Asset.Value = ( "Backward" ) |
106 | elseif inputObject.KeyCode = = Enum.KeyCode.E then |
107 | script.State.Value = ( "Emotional" ) |
108 | elseif inputObject.KeyCode = = Enum.KeyCode.One and script.State.Value = = ( "Emotional" ) then |
109 | playAnimation(Sprint) |
110 | elseif inputObject.KeyCode = = Enum.KeyCode.Space and D.Value = = true then |
113 | elseif inputObject.KeyCode = = Enum.KeyCode.Space and D.Value = = false then |
124 | game:GetService( "UserInputService" ).InputBegan:connect(onKeyPress) |
130 | player.Character.Humanoid.Running:connect(onGround) |
133 | wait(math.random( 2 , 5 )) |