Why does the Tool animation not playing when Activate?

Hello Cool Devs, I tried to play animation when the tool is activated but unfortunately it isn’t playing and give an error like below:
"ServerScriptService.knife:6: attempt to index nil with ‘Play’ - Server - knife:6"

Here is the Script and btw I m using Remote events so it fires to all the other players and can see the animation!

Local Script:


wait(0.1)
local player = game.Players.LocalPlayer 
local character = player.Character
local rs = game:GetService("ReplicatedStorage")
local event = rs.knifeEvent


local canSwing = true

local debounce = 1

script.Parent.Activated:Connect(function()
	
	local animation1 = script.Parent:FindFirstChild("attack1")
	local animation2 = script.Parent:FindFirstChild("attack2")
	local animTable = {animation1,animation2}
	local debounce = 1
	local swingAnimation = character:FindFirstChild("Humanoid"):LoadAnimation(animTable[math.random(1,#animTable)])
	
	if canSwing then
		event:FireServer(swingAnimation)	
		canSwing = false
		wait(1)
		canSwing = true
	end
end)

Server Script:

local rs = game:GetService("ReplicatedStorage")
local event = rs.knifeEvent

event.OnServerEvent:Connect(function(_,swingAnimation)
	swingAnimation:Play()
end)

Hope Some one will Help Cheers :smiley: !

is it because i m using a wait in server script? :eyes:

You have the local script and server script reversed.
(meaning the code inside the local script should be going in the server script. And the code inside the server script belongs in the local script. You had them backwards!)

The local script should be in the tool and the serverscript should be in ServerScriptService.

Hope this helped!

lol srry actually i reversed in pasting here

Ah. btw I believe there’s no need for a server script here though. Animations for players should be loaded/played from the client. aka you load and play the animation from the local script.

The only time an animation should be loaded/played from the server if it’s animating an NPC (non-playable character)

This is where you’re actually wrong, the animation must be played in a local script.

wait(0.1)
local player = game.Players.LocalPlayer 
local character = player.Character or player.CharacterAdded:Wait()


local canSwing = true

local debounce = 1

script.Parent.Activated:Connect(function()
	
	local animation1 = script.Parent:FindFirstChild("attack1")
	local animation2 = script.Parent:FindFirstChild("attack2")
	local animTable = {animation1,animation2}
	local debounce = 1
	local swingAnimation = character:FindFirstChild("Humanoid"):LoadAnimation(animTable[math.random(1,#animTable)])
	
	if canSwing then
		swingAnimation:Play()
		canSwing = false
		wait(1)
		canSwing = true
	end
end)

Hope this helps.

This should be like this, so the animation loads properly when the character is added.

local character = player.Character or player.CharacterAdded:Wait()

so no need of remote event? or

Yes no need to do that, the animation will still be visible to other players if it is played in a local script.

ok i and my frnd player.i can see his and mine anim but he is not able to see his and my anim.Why is it like that?

Is the game your currently working on under a group?
Please give me the link of the game.

1 Like

If it is, the animation should be exported to the group’s creation.

1 Like

oof it is group and anim is not group ceation

That’s why its only playing to you, because the animation is in your inventory not the group.

1 Like

oh ok, let me try to do that way!

1 Like

but in team test it is working for me but not him but in real roblox game, it is not working for both of us!

Also Humanoid:LoadAnimation() is deprecated, might wanna change it to…

local humanoid = character:FindFirstChild("Humanoid")
local Animator = humanoid:FindFirstChild("Animator")

local swingAnimation = Animator:LoadAnimation(animTable[math.random(1,#animTable)])

Might wanna update the AnimationId to the one you exported to your group.

image

when i try to publish it shows this and the anim having a yellow rounded thing

Click Select All in Explorer I think.