How to sync sounds from server to a client animation playing?

I have a item in the game that is entirely server sided, plays animations from server and sounds too, it’s flawlessly smooth. When I question my technique on it others are saying I shouldn’t play animations on server. Problem is I can’t play sounds on client to server for it to be smooth too, which makes it a problem to sync. Nor can I even use animation events from server on it because I need to use LoadAnimation() from server.

1 Like

Hello, I have figured out of a method that is delayed but does work.

Essentially, for the setup, have a remote event in Replicated Storage named whatever you want and here is the code. Keep both scripts in the tool. You can keep the animations either in the local script or in Replicated Storage too.

Server:

local replicatedStorage = game:GetService('ReplicatedStorage')
local players = game:GetService('Players')
local animationEvent = replicatedStorage.ToolAnimations

local player = players:FindFirstChild(character.Name)

local tool = script.Parent

tool.Activated:Once(function()
     animationEvent:FireClient(player)
     -- rest of code to play sounds/do effects etc
end)

Client:

repeat task.wait() until game.Players.LocalPlayer.Character -- this is to prevent breaking the code when player dies and respawns with an item

local replicatedStorage = game:GetService('ReplicatedStorage')
local players = game:GetService('Players')

local animationEvent = replicatedStorage:WaitForChild('animationEvent')

local character = players.LocalPlayer.Character or players.LocalPlayer.CharacterAdded:Wait()
local humanoid = character:WaitForChild('Humanoid')
local animator = humanoid:WaitForChild('Animator')

local tool = script.Parent
local animations = replicatedStorage:WaitForChild('Animations'):WaitForChild('Items'):WaitForChild('Tool')

local holdingAnimation = animator:LoadAnimation(animations:WaitForChild('HoldingAnimation'))
local usingAnimation=animator:LoadAnimation(animations:WaitForChild('UsingAnimation'))

tool .Equipped:Connect(function()
	holdingAnimation:Play()
end)

tool .Unequipped:Connect(function()
	holdingAnimation:Stop()
end)

foodEatEvent.OnClientEvent:Once(function()
	holdingAnimation:Stop() -- this is so holding animation doesnt overlap with usingAnimation
	usingAnimation:Play()
end)

Theres a chance there are a few typos in this code, either way enjoy.

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