Help With Playing an Animation When a Gui is Clicked

  1. What do you want to achieve? A gui when pressed, an animation will play on the player who clicked the gui.

  2. What is the issue? The animation doesn’t play and in the output it says “attempt to index nil with ‘character’”.

  3. What solutions have you tried so far? I tried studying Humanoid and Character.

My code (Workspace)

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player, animationID)
    local animation = Instance.new("Animation")
    animation.AnimationId = "http://www.roblox.com/Asset?ID="..animationID

    local loadedAnimation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(animation)
    loadedAnimation:Play()
end)

My other piece of code works but not this. Help appreaciated! Anyway, have a great day developers!

1 Like

You can’t play animations to the client on the server. You should use :FireClient() inside that code, or just play it in a local script right after it’s fired
edit: You also used

game.Players.LocalPlayer

Inside a server script, when you had “player” as a parameter.
Another thing, I don’t know if your format for AnimationId works. It might but if it doesn’t use “rbxassetid://”…animationID

1 Like

Hey there, you dont need a remote event for this.

Simply put that code into the script for your gui mouse click, simple as that! It will replicate to the other clients no problem as long as the game owner or group owns the animation.

1 Like

Oh I figured it out lol. (30 chars)

Could you post exactly how you figured it out so that others with the same problem can reference your solution? It’s not in good taste to just say “figured it out” and then mark that as the solution because it’s not a proper solution and leaves no reference for how others can fix their own similar issues.

1 Like

So basically, I added a local script and an animation to a textbutton (in screen gui, which is in starterGui.) , and here is the script:

‘’’

          wait(3)
    script.Parent.MouseButton1Click:Connect(function()
    animation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Animation)
    animation:Play()
end)

    /  '''

And thanks for letting me know @colbert2677

2 Likes

How do you make the animation stop on second click?