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) --tsk tsk what do
end
end)
end)
I'm sure the code will let you know what's wrong but if you need more info then comment.
