Hello there ! So I was trying to make a click to sync the dance animation and here is the code
--local script
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local uis = game:GetService("UserInputService")
uis.InputBegan:Connect(function(key,IsTyping)
if not IsTyping then
if key.UserInputType == Enum.UserInputType.MouseButton1 then
local Model = mouse.Target:FindFirstAncestorOfClass("Model")
if Model then
local ThePlayer = game.Players:GetPlayerFromCharacter(Model)
if ThePlayer then
print(ThePlayer)
game.ReplicatedStorage.Sync:FireServer(ThePlayer)
end
end
end
end
end)
--server script
game.ReplicatedStorage.Sync.OnServerEvent:Connect(function(player,ThePlayer)
-- play animation
local humanoid = player.Character:WaitForChild("Humanoid") -- player humanoid
local humanoid2 = ThePlayer.Character:WaitForChild("Humanoid") -- subject humanoid
local animator = humanoid:WaitForChild("Animator") -- player animator
local animator2 = humanoid2:WaitForChild("Animator") -- subject animator
local AnimationTracks = animator2:GetPlayingAnimationTracks()
local animtrack2 = animator:GetPlayingAnimationTracks()
for i,x in pairs(animtrack2) do
x:Stop()
end
wait(0.05)
print(player.Name)
print(ThePlayer.Name)
for _, v in pairs(AnimationTracks) do
--v:Stop()
if v ~= nil then
local track = animator:LoadAnimation(v.Animation)
track.TimePosition = v.TimePosition
track:AdjustSpeed(v.Speed)
track:Play()
end
end
end)
The issue is with the time position and also it only works with current dance since it does not detect if the animation of the second player is changed or not.
Please help!
You bumped it, have this. Works perfectly for me! This is a script in StarterCharacterScripts.
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Animator = Humanoid:WaitForChild("Animator")
local Mouse = LocalPlayer:GetMouse()
UserInputService.InputBegan:Connect(function(inputObj, processed)
if not processed and inputObj.UserInputType == Enum.UserInputType.MouseButton1 then
-- checks if input is mouse
local PlayerModel: Model = Mouse.Target.Parent
-- gets the Player's model
if PlayerModel:IsA("Model") then
local targetPlayer = Players:GetPlayerFromCharacter(PlayerModel)
-- checks if this is a Player
if PlayerModel:FindFirstChild("Humanoid") or targetPlayer then
-- checks if the model is a Player's character or an NPC.
-- you can modify to just players by removing "PlayerModel:FindFirstChild("Humanoid")
local targetHumanoid = PlayerModel:WaitForChild("Humanoid") :: Humanoid
local targetAnimator = targetHumanoid:WaitForChild("Animator") :: Animator
local playingAnimations = targetAnimator:GetPlayingAnimationTracks()
local currentAnimation = playingAnimations[1] :: Instance | AnimationTrack
-- gets the first playing animation, aka higher priority
if not currentAnimation then return end
-- if there's no animation, exit
local timeStamp = currentAnimation.TimePosition :: number
-- gets the current playing time
local Animation = Animator:LoadAnimation(currentAnimation.Animation) :: AnimationTrack
-- loads the animation
Animation:Play()
Animation.TimePosition = timeStamp
-- plays the animation and sets the time to the same timestamp
elseif not targetPlayer then return end
-- if it's not a player or an npc then do nothing
end
end
end)
Note: There’s no need for a RemoteEvent for the animation to play on the Server because any animation started on a player’s client will always replicate to the server, which will replicate to all clients. Hope this (very) late answer helps!
Except the animation will not stop when you walk or jump, so fix that yourself.
Additionally any animation already playing in your current Humanoid will stop. You can always stop them yourself, though. Have fun and play around!