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

Animation Code not loading fast enough?

Asked by
xPolarium 1264 Moderation Voter
1 year ago

Repost

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")

game.Players.PlayerAdded:connect(function(player)
    player.CharacterAdded:connect(function(character)

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

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

        --LOAD IDLE ANIMATION
        animation.AnimationId = "http://www.roblox.com/Asset?ID=887061813"
        local idleAnimationTrack = Humanoid:LoadAnimation(animation)       
        idleAnimationTrack:Play()

        --LETS ANIMATE THIS
        local function buttonClicked(player)
            local randomAnimation = math.random(1, #animationTable)
            if randomAnimation == previousAnimation then return end
                previousAnimation = randomAnimation
                print("Running Animation #"..randomAnimation)  
                animation.AnimationId = "http://www.roblox.com/Asset?ID="..animationTable[randomAnimation]
                local animTrack = Humanoid:LoadAnimation(animation)
                scoreValue.Value = scoreValue.Value + 1        
                animTrack:Play()
                wait(.1)
            end
    end)
end)

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
0
Answered by 1 year 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

0
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 1264 — 1y
Ad
Log in to vote
0
Answered by
Goulstem 7975 Badge of Merit Moderation Voter Administrator Community Moderator
1 year ago
Edited 1 year 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 :)

0
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 1264 — 1y
0
It's because it is using `LoadAnimation` multiple times. I'm suggesting you use it once, and take advantage of `Stop` Goulstem 7975 — 1y

Answer this question