Help with animation playing

im trying to make it so when you press e, you do an animation, but it says it can’t get the players character

here is the code:

local Shake = game.ReplicatedStorage.Shake

local Players = game:GetService("Players")
 
local player = Players.LocalPlayer
 
local character = player.character 
local humanoid = character:FindFirstChild("Humanoid")
 
local animation = Instance.new("Animation")
animation.AnimationId = "http://www.roblox.com/asset/?id=5125401222" -- Roblox dance emote
 
local animationTrack = humanoid:LoadAnimation(animation)

Shake.OnServerEvent:Connect(function(plr)
	animationTrack:Play()
end)

and I get this error

  15:10:38.359 - ServerScriptService.AnimationManagement.Shake:7: attempt to index nil with 'character'

Is this written in a server script or local script? This type of code handling animations should be run in a local script because LocalPlayer cannot be received from a server script.

oh, its in serverscriptservice, because it was run when an event was fired

Well then thats the problem. You can’t run animations on a server script and you also can’t get the player with “.LocalPlayer”. What I would suggest you doing is running the animation from the local script that fired the event instead. :slight_smile:

1 Like

thanks, just got the code inside the event and put it in my first localscript

1 Like