How to play an animation when getting hit

I want to make a tool that when you get hit, you start to sleep. I already have the tool model and animations working, but i cant figure out how to make the other person do the animation. I tried a lot of methods but none of them work. Its kind of like when you step on a part an animation plays, but instead of stepping on it you get hit by it.
This is my script

local hum = script.Parent.Parent.Humanoid
local anim = hum:LoadAnimation(script.Parent.zzzanimation)

script.Parent.Handle.Touched:Connect(function()
anim:Play()
end)

so u want sleeping animation code

You need to use remote events. Touching the part is processed on the server, but in order to play an animation you have to use a localscript. I think reading this doc will help you, especially the “Should I load an Animation on the client or server?”.

oh alright, i have the animations on a local script and people can see them but the touch part on a server script, could that be why it doesnt work?

no it just wasnt loading and there was a problem

A part’s ‘Touched’ signal will fire on the client, there’s no need for any server intervention if it isn’t required.

local Game = game
local Workspace = workspace
local Players = Game:GetService("Players")
local Player = Players.LocalPlayer

local Part = Workspace.Part
local Toggle = false

local function OnTouched(TouchedPart)
	if Toggle then return end
	local TouchedModel = TouchedPart:FindFirstAncestorOfClass("Model")
	if not TouchedModel then return end
	local TouchedHumanoid = TouchedModel:FindFirstChildOfClass("Humanoid")
	if (not TouchedHumanoid) or TouchedHumanoid.Health <= 0 then return end
	local TouchedAnimator = TouchedHumanoid:FindFirstChildOfClass("Animator")
	if not TouchedAnimator then return end
	local TouchedPlayer = Players:GetPlayerFromCharacter(TouchedModel)
	if not TouchedPlayer or (TouchedPlayer ~= Player) then return end
	Toggle = true
	local Animation = Instance.new("Animation")
	Animation.AnimationId = "rbxassetid://656118852" --Ninja run animation.
	
	local Track = TouchedAnimator:LoadAnimation(Animation)
	Track.Looped = true
	Track:Play()
end

Part.Touched:Connect(OnTouched)

The OP is implying there are two players involved; in which case, .Touched should be done on the server.

Touching the part is processed on the server, but in order to play an animation you have to use a localscript.

Both of these claims are however false, the touching of a part can be processed by the client and the server is able to load and play animations (even if it shouldn’t do).

The OP is implying there are two players involved; in which case, .Touched should be done on the server.

I disagree, the client has network ownership over their own character, if the touched event simply results in the loading and subsequent playing of an animation then everything should be handle by the client.

OP is trying to get animations to play for two people.

Therefore, a server script will be needed surely to send a signal to the other player’s localscript in order for an animation to play for them.

I want to make a tool that when you get hit, you start to sleep.

They just want to make the hit player’s character perform a ‘sleep’ animation.

local Game = game
local Workspace = workspace
local Players = Game:GetService("Players")
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:FindFirstChildOfClass("Humanoid") or Character:WaitForChild("Humanoid")
local Animator = Humanoid:FindFirstChildOfClass("Animator") or Humanoid:WaitForChild("Animator")

local function OnHumanoidTouched(TouchingPart, HumanoidPart)
	if TouchingPart.Name == "Projectile" then --Check if touching part is a 'projectile'.
		--Load and play animation here.
	end
end

Humanoid.Touched:Connect(OnHumanoidTouched)
1 Like

its basically like a sword but instead of the other person taking damage they perform a sleeping animation

what i said above is most of the context ^