This is a relatively simple problem, so I don’t think I need too much context.
Code, it is ran on server.
The animation id is the animation, the Animator, i haven’t touched at all.
code
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
script.Parent.Touched:Connect(playAnimationFromServer)
This is all inside of a regular Block rig I put in.
As @krasycheckz correctly said, you aren’t providing the correct parameters to the playAnimationFromServer function. The only parameter which will be automatically passed is hit which is the part which interacted with script.Parent. Instead you should make sure the part is a character part and pass the character and animation to the function:
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://your_animation_id"
script.Parent.Touched:Connect(function(hit)
--to ignore npcs, replace the condition with game.Players:GetPlayerFromCharacter(hit.Parent)
if hit.Parent:FindFirstChildWhichIsA("Humanoid") then
playerAnimationFromServer(hit.Parent, animation)
end
end)
I rewrote the code (Based on what I had earlier) and this is what my rig looks like.
Code:
local character = script.Parent
local animation
local humanoid = script.Parent.Humanoid
if humanoid then
-- need to use animation object for server access
local animator = humanoid:FindFirstChildOfClass("Animator")
if animator then
local animationTrack = animator:LoadAnimation(script.Parent.Humanoid.Animation)
animationTrack:Play()
return animationTrack
end
end