Still have questions? Join our Discord server and get real time help.
Log in to vote

Animation Code not loading fast enough?

Asked by
xPolarium 1358 Moderation Voter
2 years ago


So I need some help with efficiency problems with a "Play Animation on click" script. It's a really simple part-click script that changes the animation of a NPC to another random animation. The real issue is that there is a random delay that appears trying to load the next animation. What I need it to do is let the player be able to spam the button, increase the score and change the animations smoothly.

What I think it is, it's something to do with the single wait(.1) I have at the bottom of the code, but I have no idea if it's really the case or how to change it to something better. Also I doubt my animations have delays in them since its 5 with the same timeline lengths, priority and KeyFrames.

local runService = game:GetService("RunService")

local starterGui = game:GetService('StarterGui')
starterGui:SetCore("TopbarEnabled", false)
local scoreValue = script:FindFirstChild("Score")


        if not character:FindFirstChild("Humanoid") then return end
        local humanoid = character:WaitForChild("Humanoid")
        humanoid.WalkSpeed = 0

        local animationTable = {887079940,887088637,887093321,887096360,887101694}
        local clickOnMe = game:GetService("Workspace").ClickOnMe
        local Humanoid = game.Workspace.Noob:WaitForChild("Humanoid")
        local animation ="Animation", game.Workspace.Noob)
        local previousAnimation;

        animation.AnimationId = ""
        local idleAnimationTrack = Humanoid:LoadAnimation(animation)       

        local function buttonClicked(player)
            local randomAnimation = math.random(1, #animationTable)
            if randomAnimation == previousAnimation then return end
                previousAnimation = randomAnimation
                print("Running Animation #"..randomAnimation)  
                animation.AnimationId = ""..animationTable[randomAnimation]
                local animTrack = Humanoid:LoadAnimation(animation)
                scoreValue.Value = scoreValue.Value + 1        

I'm sure the code will let you know what's wrong but if you need more info then comment.

2 answers

Log in to vote
Answered by 2 years ago

This might be the reason, but i'm not sure. Your animation time might be long, if you're using Roblox's animation editor, open it, then open your animation, go to Edit, then change animation length to a shorter time. It might fix the problem

The thing is. I don't think it's that. I checked everything on my timelines and everything is basically the same length for all my animations. I'll give it another go but only to check my Animation Priorities. xPolarium 1358 — 2y
Log in to vote
Answered by
Goulstem 8047 Badge of Merit Moderation Voter Administrator Community Moderator
2 years ago
Edited 2 years ago

You could try to use a combination of animation:Stop() and animation:Play(), so that your animation never ends, and you only have to load each one once :)

What I'm looking for is; I want it to change animation when the player clicks (I don't need the animation to fully play). This script does that but has weird pauses as if it's causing lag issues. Memory leak? xPolarium 1358 — 2y
It's because it is using `LoadAnimation` multiple times. I'm suggesting you use it once, and take advantage of `Stop` Goulstem 8047 — 2y

Answer this question