Hello! So I have a tool which opens a UI (singular button). When you click the button, the local script fires an event to a server script (located in ServerScriptService) which plays the animation provided. The animations loop in order.
The problem: My animations are not played. Nothing happens. No errors.
Local Script
local player = game.Players.LocalPlayer
local event = game:GetService("ReplicatedStorage"):WaitForChild("animSwitch")
-- Animations removed for DevForum
local animations = {
[1] = "rbxassetid://[]",
[2] = "rbxassetid://[]",
[3] = "rbxassetid://[]",
[4] = "rbxassetid://[]",
}
local currentAnim = nil
script.Parent.MouseButton1Click:Connect(function()
if currentAnim == nil then
event:FireServer(animations[1])
currentAnim = 1
print("Playing animation "..currentAnim)
elseif currentAnim ~= nil then
local nextAnim, value = next(animations, currentAnim)
event:FireServer(value)
currentAnim = nextAnim
print("Playing animation "..currentAnim)
end
end)
Server Script
game:GetService("ReplicatedStorage"):WaitForChild("animSwitch").OnServerEvent:Connect(function(player, animationId)
local character = player.Character
local humanoid = character.Humanoid
if not humanoid then warn("no humanoid") end
local batonAnimation = Instance.new("Animation")
batonAnimation.AnimationId = animationId
local batonTrack = humanoid:LoadAnimation(batonAnimation)
batonTrack:Play()
end)
The code prints stuff into the output, but nothing happens. Does anyone know why?
You are able to play animations from the client, and have it replicated to the server. I would recommend putting the animation “handlers” (loading the animation & playing it) from the server script into the client-sided script.
Does the animation replicate directly to the server if I play it from the client, or is there something I need to do in order to replicate the animation to the server?
Also, in terms of handling animations, you mean loading them into an animation instance within the client-sided script rather than the server script?
By “handling animations”, I mean putting this part
local character = player.Character
local humanoid = character.Humanoid
if not humanoid then warn("no humanoid") end
local batonAnimation = Instance.new("Animation")
batonAnimation.AnimationId = animationId
local batonTrack = humanoid:LoadAnimation(batonAnimation)
batonTrack:Play()
from the server script into the local script.
Obviously, please remember to change and set variables where necessary.
Right, so I’ve edited my local script so it contains the following:
local player = game.Players.LocalPlayer
-- local event = game:GetService("ReplicatedStorage"):WaitForChild("animSwitch")
local animations = {
[1] = "rbxassetid://[]",
[2] = "rbxassetid://[]",
[3] = "rbxassetid://[]",
[4] = "rbxassetid://[]",
}
local currentAnim = nil
local function playAnimation(animationId)
local character = player.Character
local humanoid = character.Humanoid
if not humanoid then warn("no humanoid") end
local batonAnimation = Instance.new("Animation")
batonAnimation.AnimationId = animationId
local batonTrack = humanoid:LoadAnimation(batonAnimation)
batonTrack:Play()
end
script.Parent.MouseButton1Click:Connect(function()
if currentAnim == nil then
playAnimation(animations[1])
currentAnim = 1
print("Playing animation "..currentAnim)
elseif currentAnim ~= nil then
local nextAnim, value = next(animations, currentAnim)
playAnimation(value)
currentAnim = nextAnim
print("Playing animation "..currentAnim)
end
end)
When running the game and clicking the UI, nothing happens, but the output says it’s playing an animation.
Yep, did both of those and I’m still not getting any results… :((
local function playAnimation(animationId)
local character = player.Character
local humanoid = character.Humanoid
if not humanoid then warn("no humanoid") end
local batonAnimation = Instance.new("Animation")
batonAnimation.Parent = humanoid
batonAnimation.AnimationId = animationId
local batonTrack = humanoid:LoadAnimation(batonAnimation)
batonTrack:Play()
end
Here is a code snippet from the Animator documentation which may prove helpful:
local function playAnimationFromServer(character, animation)
local humanoid = character:FindFirstChildOfClass("Humanoid")
if humanoid then
-- need to use animation object for server access
local animator = humanoid:FindFirstChildOfClass("Animator")
if animator then
local animationTrack = animator:LoadAnimation(animation)
animationTrack:Play()
return animationTrack
end
end
end
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://507765644"
local character = script.Parent
playAnimationFromServer(character, animation)
local function playAnimation(animationId)
local character = player.Character
local humanoid = character.Humanoid
if not humanoid then warn("no humanoid") end
local animator = humanoid:WaitForChild("Animator")
if animator then
local batonAnimation = Instance.new("Animation")
batonAnimation.Parent = humanoid
batonAnimation.AnimationId = animationId
local batonTrack = animator:LoadAnimation(batonAnimation)
batonTrack:Play()
print("Playing animation "..currentAnim)
end
end
I’ve adjusted my code to the following, and surprisingly the code prints the string at the end, meaning the animator exists, yet the actual animation still doesn’t play… I’m confused lol.