I’m trying to create a GUI that has multiple different buttons that play multiple different animations, similar to TTD3 or Vibe NYC. Although, it seems that it’s not working as intended. No errors appear in the output, and since I’m not a very good programmer, I can’t figure out the problem myself. I’ve seen from other posts that animations can be a bit strange when programming, and now I see why.
What is wrong with this script?
-- local script
local p = script.Parent
local plr = game.Players.LocalPlayer
repeat wait() until plr.Character
local char = plr.Character
local humanoid = char:WaitForChild("Humanoid")
local animation
function playAnimation(id)
if char ~= nil and humanoid ~= nil then
local id = "rbxassetid://".. tostring(id)
local oldAnimation = char:WaitForChild("LocalAnimation")
if animation ~= nil then
animation:Stop()
end
if oldAnimation ~= nil then
if oldAnimation.AnimationId == id then
oldAnimation:Destroy()
return
end
oldAnimation:Destroy()
end
local newAnimation = Instance.new("Animation", char)
newAnimation.Name = "LocalAnimation"
newAnimation.AnimationId = id
animation = humanoid:LoadAnimation(newAnimation)
animation:Play()
end
end
local BTN1 = p.BTN1
BTN1.MouseButton1Click:Connect(function()
playAnimation(BTN1.AnimationID.Value)
end)
-- the animation id i'm using for BTN1 is "6985706275"
I tried simplifying it, but it still doesn’t work. Now, that could be my scripting skills since I’m not very good, but here’s what I changed the script to in hopes that it’d work.
local p = script.Parent
local plr = game.Players.LocalPlayer
local char = plr.Character
local humanoid = char:WaitForChild("Humanoid")
local animation = Instance.new("Animation", char)
animation.Name = "Animation"
function playAnim1()
if char ~= nil and humanoid ~= nil then
local id = "rbxassetid://6985706275"
animation.AnimationId = id
local newAnimation = humanoid:LoadAnimation(animation)
newAnimation:Play()
end
end
local BTN1 = p.BTN1
BTN1.MouseButton1Click:Connect(function()
playAnim1()
end)
Are there any visible problems? The only output from this script is the line that says “playAnim1()” in the click area, which leads me to think that there’s obviously something wrong with the main function.
That’s not true, as far as I know. I’ve been able to use animations made by other people. Humanoid:LoadAnimation() is deprecated. Read this post for more information.
Can you let me know if the animations are under the experience’s owner? If the experience is owned by a group, the animation must be uploaded to the group that owns the experience. The same goes for experiences that are owned directly by your Roblox profile. If so, the animation must be uploaded and owned by you. Hope this helps.