Playing an animation on a character that's been hit

So i’ve been trying to make a hit animation for the player that’s been damaged but, i do not know how to make the hit character play the animation, please help.Script is down below

local rp = game:GetService(“ReplicatedStorage”)
local InfiniteLoop = rp:WaitForChild(“GoldExperienceRequiemRemotes”):WaitForChild(“InfiniteLoop”)
local debris = game:GetService(“Debris”)
local TweenService = game:GetService(“TweenService”)

local damage = 10 --ammount of damage it will do

InfiniteLoop.OnServerEvent:Connect(function(Player)
local Character = Player.Character
local Humanoid = Character:WaitForChild(“Humanoid”)
local HumanoidRP = Character:WaitForChild(“HumanoidRootPart”)

local Stand = workspace:FindFirstChild(Player.Name.." Stand"):FindFirstChild("Gold Experience Requiem")

local Folder = Instance.new("Folder", workspace)
Folder.Name = Player.Name.." Infinite Loop"
debris:AddItem(Folder,.5)

if Stand then
	local prevWeld = Stand:WaitForChild("HumanoidRootPart"):WaitForChild("Stand Weld")
	prevWeld:Destroy()
	
	Stand:WaitForChild("HumanoidRootPart").CFrame = HumanoidRP.CFrame * CFrame.new(0,0,-2.5)
	
	local weld = Instance.new("ManualWeld") --creates a new weld to move our stand forward
	weld.Name = "Stand Weld"
	weld.Part0 = Stand:WaitForChild("HumanoidRootPart")
	weld.Part1 = HumanoidRP
	weld.C0 = Stand:WaitForChild("HumanoidRootPart").CFrame:inverse() * HumanoidRP.CFrame
	weld.Parent = weld.Part0
	
	local AnimControl = Stand:WaitForChild("AnimControl")
for i, track in pairs(AnimControl:GetPlayingAnimationTracks()) do  --stops previous animations
	track:Stop()
	end
	
	local Punch = AnimControl:LoadAnimation(script:WaitForChild("Punch"))
	Punch:Play()  --plays the animation
	
	local wry = Instance.new("Sound")
	wry.Volume = 1
	wry.SoundId = "rbxassetid://2821732625"
	wry.Parent = game.Workspace
	wry:Play()
	debris:AddItem(wry,4)
	
	local Hitbox = script:WaitForChild("Hitbox"):Clone()
	Hitbox.Parent = Folder
	Hitbox.CFrame = Stand:WaitForChild("Right Arm").CFrame + Vector3.new(0,-1,0) --use RightHand if you're using r15 (delete + vector3.new also)
	
	local hweld = Instance.new("ManualWeld")
	hweld.Part0 = Hitbox
	hweld.Part1 = Stand:WaitForChild("Right Arm")
	hweld.C0 = hweld.Part0.CFrame:inverse() * hweld.Part1.CFrame
	hweld.Parent = hweld.Part0
	
	Hitbox.Touched:Connect(function(Hit)
		if Hit:IsA("Part") or Hit:IsA("MeshPart") then  --checks if what our stand hit was a character/humanoid
			if Hit.Parent ~= Character then
				local EHumanoid = Hit.Parent:FindFirstChild("Humanoid")
				local EHumRP = Hit.Parent:FindFirstChild("HumanoidRootPart")
				
				if EHumanoid and EHumRP then
				   Hitbox:Destroy()
				
				    EHumanoid:TakeDamage(damage)	--makes the humanoid we just hit, take damage
			        EHumanoid.WalkSpeed = 0
			        EHumanoid.JumpPower = 0
                    local Sound = Instance.new("Sound",Stand)
                       Sound.Volume = 3
                       Sound.SoundId = "rbxassetid://4567255304"  --set here the id of the sound you want played when the character is hit
                       Sound:Play()
                       debris:AddItem(Sound,3)

                local wave = script:WaitForChild("Wave"):Clone()
				wave.Parent = Folder
				wave.CFrame = Hit.CFrame
				wave.Orientation = wave.Orientation + Vector3.new(90,0,0)
				debris:AddItem(wave,.55)
				
				local goal = {}
				goal.Size = wave.Size + Vector3.new(5,0,5)
				goal.Transparency = wave.Transparency + (1 - wave.Transparency)
				local info = TweenInfo.new(.25)
				local tween = TweenService:Create(wave,info,goal)
				tween:Play()                           

                     local vel = Instance.new("BodyVelocity",EHumRP)
                     vel.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
                     vel.Velocity = HumanoidRP.CFrame.lookVector * 75
                     debris:AddItem(vel,.2)
				end
			end
		end
	end)
	wait(1)
	
	local prevWeld = Stand:WaitForChild("HumanoidRootPart"):WaitForChild("Stand Weld")
		prevWeld:Destroy()
		
		Stand:WaitForChild("HumanoidRootPart").CFrame = HumanoidRP.CFrame * CFrame.new(2,0,2)
		
		local weld = Instance.new("ManualWeld")
		weld.Name = "Stand Weld"
		weld.Part0 = Stand:WaitForChild("HumanoidRootPart")
		weld.Part1 = HumanoidRP
		weld.C0 = Stand:WaitForChild("HumanoidRootPart").CFrame:inverse() * HumanoidRP.CFrame
		weld.Parent = weld.Part0
		
		local idle = AnimControl:LoadAnimation(script:WaitForChild("Idle"))
		idle:Play()
	
	
	InfiniteLoop:FireClient(Player)
	
end

end)

edit: the script is now complete

1 Like

Your script is not complete. Please edit your post.

ID = Asset ID
EHumanoid = Humanoid selected

Animation = EHumanoid:LoadAnimation(ID)
Animation:Play()

Edited, now it’s complete, but why did you specifically want it full?

typed this in
ID = “rbxassetid://13445679”
EHumanoid = EHumanoid
Animation = EHumanoid:LoadAnimation(ID)
Animation:Play()
says it’s unable to cast value to object

Sorry, huge mistake from my part, You cannot load the ID Directly,
so it has to be EHumanoid:LoadAnimation(Instance) instead

Instance is the Animation object, so either you have a existing one and just select it, or make it directly from the script, which is just

local Anim = Instance.new("Animation")
Anim.AnimationId = ID
EHumanoid:LoadAnimation(Anim)
Anim:Play()

For some reason, it says that “Play” Isn’t part of “Anim” i do not know why

image

Yikes, failed twice, though i also assumed you could fix as the code above was a quick example, i didn’t even defined LoadAnimation as a variable you could invoke

local Anim = Instance.new("Animation")
Anim.AnimationId = ID
local Animation = EHumanoid:LoadAnimation(Anim)
Animation:Play()