How should I approach creating an animation system that supports syncing?

Hey developers! I am currently trying to create a dance system that supports syncing.

If you’re unaware of what this is, when a player syncs with another their “dances” are exactly the same (same dance, same time position) until they unsync. I’ve started to try to make this script, but I haven’t got very far:

local Players = game:GetService("Players")
local MarketplaceService = game:GetService("MarketplaceService")
local ReplicatedStorage =  game:GetService("ReplicatedStorage")

local AnimationsModule = require(ReplicatedStorage:WaitForChild("Modules"):WaitForChild("Animations"))

local EventsFolder = ReplicatedStorage:WaitForChild("Events").Animations

local Remotes = {
	Sync = EventsFolder:WaitForChild("Sync");
	Dance = EventsFolder:WaitForChild("Dance");
}

local Gamepasses = require(ReplicatedStorage:WaitForChild("Modules"):WaitForChild("Gamepasses"))


local SyncGroups = {} -- [Player.UserId] = {PlayersThatAreSynced}

local function PlayerAdded(Player)
	SyncGroups[Player.UserId] = {}
end

for _, Player in pairs(Players:GetChildren()) do
	PlayerAdded(Player)
end

Players.PlayerAdded:Connect(PlayerAdded)

Players.PlayerRemoving:Connect(function(Player)
	SyncGroups[Player.UserId] = nil

	for Id, _ in pairs(SyncGroups) do
		local Found = table.find(SyncGroups[Id], Player)
		if Found then
			table.remove(SyncGroups[Id], Found)
		end
	end
end)

function Dance(Player, AnimationId)
	local Character  = Player.Character or Player.CharacterAdded:Wait()

	local CharactersToAnimate = {Character}

	local Focused  = SyncGroups[Player.UserId]


	for I, Player in pairs(Focused) do

		if Player.Character then table.insert(CharactersToAnimate, Player.Character) end

	end

	for _, Character in pairs(CharactersToAnimate) do
		-- ?!?!?!?!?
	end
end


Remotes.Sync.OnServerEvent:Connect(function(Player, ToSyncWith)
	
end)

Remotes.Dance.OnServerEvent:Connect(function(Player, AnimationName)
	local AnimationId = AnimationsModule[AnimationName]
	if AnimationId then
		Dance(Player, AnimationId) 
	end
end)

The main thing I am struggling with is the SyncGroups logic and the actual animating. Does anyone have any help to offer? It’s appreciated.

1 Like

Still looking for a solution btw. :smiley:

Are you trying to just make two players play the animation at the same time?

If that’s the case then try play the animation for both players on the server since it wont rely on the client to play the animation which means the animations should play in sync.

I recently came across creating an animation syncing system. If you want this system to be more complex and support different players syncing each other, you can approach this with an object-oriented approach.

In terms of your code, where you loop through the ‘CharactersToAnimate’ table, get the Animator object from the character’s humanoid. You can do by getting the ‘Animator’ object as it is a child of the Humanoid instance. Use :LoadAnimation() after creating an Animation instance with the given animation id, and pass in the animation instance. (Animator:LoadAnimation(AnimationInstance)). This will return an AnimationTrack object which you can then use :Play() on.

Example:

function Animation:PreloadAnimation(AnimationNumber)
    local AnimInfo = AnimationList[AnimationNumber];
    if not self.AnimationTracks[AnimationNumber] and AnimInfo then
        local Character = self.Player.Character;
        if Character then
            local Humanoid = Character:FindFirstChildOfClass('Humanoid');
            if Humanoid then
                local Animator = Humanoid:FindFirstChildOfClass('Animator');
                if Animator then
                    local AnimInstance = Instance.new('Animation');
                    AnimInstance.AnimationId = 'rbxassetid://'.. AnimInfo.AnimationId;
                    self.AnimationTracks[AnimationNumber] = Animator:LoadAnimation(AnimInstance);
                    AnimInstance:Destroy();
                end
            end
        end
    end
end

This is simply an example from what I have created. Use it as it fits your needs.

2 Likes

No, one player is dancing, and any other player has the option to “sync” with that player: then they have the same animation at the same time.