Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to make an RPG npc loads an animation when attacking?

i wanted to make an npc that loads an animation when attacking but nothing worked, and i need it in one script because i have to make a delay for the attack, thank you.

1 comment

0

Can you include your script in a code block.

1 answer

Heres the code i am using that doesn't work of course

local larm = script.Parent:FindFirstChild("Left Arm")
local rarm = script.Parent:FindFirstChild("Right Arm")

function findNearestTorso(pos)
	local list = game.Workspace:children()
	local HumanoidRootPart = nil
	local dist = 100
	local temp = nil
	local human = nil
	local temp2 = nil
	for x = 1, #list do
		temp2 = list[x]
		if (temp2.className == "Model") and (temp2 ~= script.Parent) then
			temp = temp2:findFirstChild("HumanoidRootPart")
			human = temp2:findFirstChild("Humanoid")
			if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
				if (temp.Position - pos).magnitude < dist then
					HumanoidRootPart = temp
					dist = (temp.Position - pos).magnitude
				end
			end
		end
	end
	return HumanoidRootPart
end

function Hit(hit)
	local human = hit.Parent:FindFirstChild("Humanoid")
	if human ~= nil then
		local animation = Instance.new("Animation")
		animation.AnimationId = "http://www.roblox.com/Asset?ID=714140890" -- your id here
 
		local animController = Instance.new("AnimationController")
		local animTrack = animController:LoadAnimation(animation)
		animTrack:Play()
		human.Health =- 30
		wait(4.5)
	end
end

larm.Touched:connect(Hit)
rarm.Touched:connect(Hit)

while true do
	wait(0.1)
	local target = findNearestTorso(script.Parent.Torso.Position)
	if target ~= nil then
		script.Parent.Zombie:MoveTo(target.Position, target)
	end
end

This discussion is preserved as it was. New answers, comments, and votes are closed.