Animations not synced between players

I have a simple running animation controller, everything works fine on my own screen, but on other people’s screen, the animations never stop even when it has stopped on my client.

How to reproduce: (Replace with own animation Ids)

local RunService = game:GetService("RunService")
local Players = game:GetService("Players")

local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

-- Create a new animation instance for running
local newRunAnimation = Instance.new("Animation")
local playRun  -- This will hold the AnimationTrack

-- Function to update the AnimationId based on player role
local function updateAnimationId()
	local previousAnimationId = newRunAnimation.AnimationId  -- Store current AnimationId

	-- Determine the new AnimationId based on player role
	if player:FindFirstChildOfClass("ObjectValue") == nil then
		newRunAnimation.AnimationId = "rbxassetid://180426354" -- Default animation
	elseif player:FindFirstChild("test") then
		newRunAnimation.AnimationId = "rbxassetid://115846971274296" -- Police run animation
	elseif player:FindFirstChild("test2") then
		newRunAnimation.AnimationId = "rbxassetid://96621188017208" -- Fireman run animation
	else
		return  -- Exit if no valid role is found
	end

	-- Reload the animation only if the AnimationId has changed
	if newRunAnimation.AnimationId ~= previousAnimationId then
		if playRun then
			print("stopping")
			playRun:Stop()  -- Stop the old animation if it's playing
		end
		playRun = humanoid:LoadAnimation(newRunAnimation)  -- Reload with new AnimationId
	end
end

-- Call updateAnimationId once to initialize AnimationId
updateAnimationId()

-- Continuously check player movement and role to play/stop the animation
RunService.Heartbeat:Connect(function()
	updateAnimationId()  -- Update the animation ID based on role if necessary

	-- Check if the player is moving
	if humanoid.MoveDirection.Magnitude > 0.1 and (not playRun or not playRun.IsPlaying) then
		print("starting")
		playRun:Play()
	elseif humanoid.MoveDirection.Magnitude <= 0.1 and playRun and playRun.IsPlaying then
		print("stopping")
		playRun:Stop()
	end
end)

Next, create parts and insert the script (change tagName) to change the ObjectValue of the player (to change animations) on hit:

-- Define the tag name
local tagName = "test"

-- Function to add the tag to the player
local function onTouch(hit)
	-- Check if the object that touched the part is a player
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)

	if player then
		if player:FindFirstChildOfClass("ObjectValue") then
			local tagToDelete = player:FindFirstChildOfClass("ObjectValue")
			tagToDelete:Destroy()
		end
		-- Check if the player already has the tag
		if not player:FindFirstChild(tagName) then
			-- Create the tag if it doesn't exist
			local tag = Instance.new("ObjectValue")
			tag.Name = tagName
			tag.Value = player -- Set the value to the player itself
			tag.Parent = player

			print(player.Name .. " has been given the " .. tagName .. " tag.")
		end
	end
end

-- Connect the onTouch function to the part's Touched event
script.Parent.Touched:Connect(onTouch)

I believe this is a roblox bug, since the script should work. Could anyone explain why this is?

2 Likes

Just a forward, Roblox animation replication is riddled with long standing bugs and inconsistencies with the documentation, so it definitely might be possible this is just Roblox’s fault.


I would try switching your code to use separate Animation instances for each of the assets, it’s kind of a weird pattern to be changing the AnimationId on the fly. (I just haven’t seen this done before, it doesn’t logically seem like it should cause problems though, and the docs don’t mention any reason not to do this.)

I would also verify that your character has an Animator instance, which is required for animations to replicate.

Another note is that a lot of properties of AnimationTracks like priority and stuff don’t replicate, even if the docs say otherwise (they might have updated that by now).

Humanoid:LoadAnimation is also depreciated in favor of Animator:LoadAnimation. Per the docs:

You should use this function directly instead of the similarly-named Humanoid:LoadAnimation() and AnimationController:LoadAnimation() functions. These are deprecated proxies of this function which also create an Animator if one does not exist; this can cause replication issues if you are not careful.

2 Likes

You’ve nailed it, as I’ve actually experienced this issue before and using separate Animation instances instead of changing the AnimationId of a single Animation instance fixed the issue:

While the problem was still active for me, it resulted in players seeing other players dance while animations were working correctly for their own character, so reusing an Animation does seem to mess with their replication

3 Likes

Life-saver, i found that there is a easier solution which is to reset the player every time the animationId changes, but your method is definitely more flexible. I’ll be trying it out, thanks!

2 Likes

I think @BendsSpace deserves the solution more than I do, since they noticed what was the cause of the problem before I did

I’m glad that you were able to fix the issue, though :slight_smile::+1:

1 Like

Fixed, thanks for being considerate!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.