I have a script for an npc AI to attack people, but it only functions once even though I have my key frames labeled in the animation. Does it have to do with the animation itself or is it the code? Any help fixing this would be appreciated as the game would be too easy without getting hurt by the enemies.
local animation_attack = Instance.new("Animation")
animation_attack.AnimationId = "http://www.roblox.com/asset/?id=736297595"
local running_animation_attack = nil
function attack (human)
game:GetService("ContentProvider"):Preload(animation_attack.AnimationId)
sound(181268614,linked_sword.Handle,math.random(90,110)/100,1,2)
if(running_animation_attack == nil and human.Health ~= 0) then
local event_keyframe_reached
running_animation_attack = humanoid:LoadAnimation(animation_attack)
running_animation_attack:Play()
event_keyframe_reached = running_animation_attack.KeyframeReached:connect(function(keyframeName) -- Bind function to KeyframeReached event
if (keyframeName == "Hit") then
hurt(human)
elseif(keyframeName == "End") then
running_animation_attack:Stop()
running_animation_attack = nil
event_keyframe_reached:disconnect()
end
end)
repeat wait(0.1) until running_animation_attack == nil
end
end

1 comment
You should tab your code correctly.