Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local pirate = script.Parent
- local humanoid = pirate:WaitForChild("Humanoid")
- local rootPart = pirate:WaitForChild("HumanoidRootPart")
- local npcFolder = workspace:WaitForChild("NPC")
- local dataFolder = workspace:WaitForChild("Data")
- local attackSpeedValue = npcFolder:WaitForChild("Attackspeed")
- local playerValue = dataFolder:WaitForChild("Player")
- local ATTACK_DISTANCE = 5
- local DAMAGE = 10
- local HIT_DELAY = 0.25
- local animator = humanoid:FindFirstChildOfClass("Animator")
- if not animator then
- animator = Instance.new("Animator")
- animator.Parent = humanoid
- end
- -- Ищем анимацию атаки
- local attackAnimation = nil
- -- Сначала ищем Animation с названием Attack
- for _, object in ipairs(pirate:GetDescendants()) do
- if object:IsA("Animation") and object.Name == "Attack" then
- attackAnimation = object
- break
- end
- end
- -- Если Attack нет, ищем Slash.
- -- У ClassicSword обычно именно такая анимация.
- if not attackAnimation then
- local sword = pirate:FindFirstChild("ClassicSword")
- if sword then
- attackAnimation = sword:FindFirstChild("Slash")
- if not attackAnimation then
- for _, object in ipairs(sword:GetDescendants()) do
- if object:IsA("Animation") and object.Name == "Slash" then
- attackAnimation = object
- break
- end
- end
- end
- end
- end
- -- Последняя попытка — найти любую Animation внутри Pirate
- if not attackAnimation then
- for _, object in ipairs(pirate:GetDescendants()) do
- if object:IsA("Animation") then
- attackAnimation = object
- break
- end
- end
- end
- local attackTrack = nil
- if attackAnimation then
- attackTrack = animator:LoadAnimation(attackAnimation)
- attackTrack.Priority = Enum.AnimationPriority.Action
- else
- warn("У Pirate не найдена анимация атаки!")
- end
- while pirate.Parent do
- if humanoid.Health <= 0 then
- break
- end
- local player = playerValue.Value
- if not player then
- task.wait(0.2)
- continue
- end
- local character = player.Character
- if not character then
- task.wait(0.2)
- continue
- end
- local targetHumanoid = character:FindFirstChildOfClass("Humanoid")
- local targetRoot = character:FindFirstChild("HumanoidRootPart")
- if not targetHumanoid or not targetRoot or targetHumanoid.Health <= 0 then
- task.wait(0.2)
- continue
- end
- local distance = (targetRoot.Position - rootPart.Position).Magnitude
- if distance <= ATTACK_DISTANCE then
- -- Сообщаем SeekEnemy, что сейчас атакуем
- pirate:SetAttribute("IsAttacking", true)
- -- Поворачиваем пирата лицом к игроку
- local lookPosition = Vector3.new(
- targetRoot.Position.X,
- rootPart.Position.Y,
- targetRoot.Position.Z
- )
- rootPart.CFrame = CFrame.lookAt(
- rootPart.Position,
- lookPosition
- )
- -- Запускаем анимацию
- if attackTrack then
- attackTrack:Play()
- end
- -- Небольшая задержка перед попаданием
- task.wait(HIT_DELAY)
- -- Проверяем расстояние еще раз,
- -- потому что игрок мог отойти во время замаха
- if targetHumanoid.Health > 0 and targetRoot.Parent then
- local newDistance = (
- targetRoot.Position - rootPart.Position
- ).Magnitude
- if newDistance <= ATTACK_DISTANCE then
- targetHumanoid:TakeDamage(DAMAGE)
- end
- end
- -- Ждем окончания анимации
- if attackTrack then
- local animationLength = attackTrack.Length
- if animationLength > 0 then
- task.wait(math.max(animationLength - HIT_DELAY, 0))
- else
- task.wait(0.2)
- end
- else
- task.wait(0.2)
- end
- pirate:SetAttribute("IsAttacking", false)
- -- Скорость атаки.
- -- Например:
- -- 1 = атака раз в секунду
- -- 2 = две атаки в секунду
- -- 4 = четыре атаки в секунду
- local attackSpeed = math.max(attackSpeedValue.Value, 1)
- local cooldown = 1 / attackSpeed
- task.wait(cooldown)
- else
- task.wait(0.1)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment