Need an idea on how to play an animation out of a table

If the title wasn’t enough, here’s a more in-depth summary: I have a list of animations that I want to play from a GUI, when the player clicks on one of the buttons, it fires a remote. In the server script, I have a table of animations, which you will see in the code below. I know that I have to use a for loop to go through the table, but the problem is i’m not so sure how that would look like, and I’ve been stuck on it for hours, trying to find a thread that showed how.
Serverscript:

local animations = {
	script.Animation1,
	script.Animation2
}

local RemoteEvent = game:GetService("ReplicatedStorage").Animations.Arms
local toggle = true

RemoteEvent.OnServerEvent:Connect(function(player)
	
	local hum = player.Character:WaitForChild("Humanoid")
	local load = hum:LoadAnimation(animations[1], animations[2])
	
	for i, v in pairs(animations) do
		v:Play()
	end
end)

Image of the animation setup:
image

Localscript that fires the remote:

local button = script.Parent
local RemoteEvent = game:GetService("ReplicatedStorage").Animations.Arms

button.MouseButton1Click:Connect(function()
	RemoteEvent:FireServer()
end)

Edit: I forgot to add this but, I need this to remain as a local script because I’d be pausing a few of my animations on a key frame, and that needs to be replicated through the server or else only the client will see the paused animation.

1 Like

Animations should be played locally. Players have network ownership of their character and should be the ones to play the animation instead of the server.

As an alternative, you can use a RemoteFunction to return the AnimationId of the animation instance, and have the client play it. If your concern is replication, animations played on the client will replicate to other clients.

I forgot this detail, and i’ll edit it in a second, however, I need it to stay as a serverscript because I realized and found out that pausing the animation on a keyframe through a script needs to be replicated through the server.

Why? Are you waiting for a keyframe to be reached to create an effect or something like that?

Yes, I’m waiting for the animation to reach a certain keyframe so I can pause it.

Effects should not be made on the server. The server has no business making effects because it causes extra strain on the server and the effect will look choppy on clients that have poor connections. I used to do this before I was pointed in the right direction:

  1. Client sends request to server (e.g. a punch request, ability request)
  2. Server receives request
    2a. Does sanity checking to determine if the player can actually do the requested action
    2b. If the sanity checking is all clear, then:
    2c. Does collision detection
  3. Server FireAllClients(), passing the player who requested the action as an argument
  4. All Clients receive the request
    4a. If the Client == player who requested the action then do the animation processing and effect
    4b. If the Client ~= player who requested the action then wait() for a certain amount of time (time when the keyframe is reached in the animation, e.g. if the keyframe is at 0.2 seconds then wait(0.2)) then also create the effect.
1 Like

You can use :GetMarkerReachedSignal on any animation track, even for other players in a local script. This requires you to get the humanoid of all current players. The humanoid object has an event Humanoid.AnimationPlayed that returns the animation track playing on the humanoid. You can use this to get the animation track and check for a marker reached event.

You should not be doing visual effects on the server, instead play the animation on the client and do the visuals on the client. You can have other clients listen for the animation being played and do the visual effects themselves as well.

I understand what you’re trying to say, and I appreciate the feedback, and for the next time I edit my script, I’ll switch it over to the client, but… my original question is on my post. That being what it would look like to check through a table and play a certain animation after the button is clicked.

Yes, I know about the :GMRS function already, that wasn’t my original question.