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 sword = pirate:WaitForChild("ClassicSword")
- local ATTACK_DISTANCE = 6
- local DAMAGE = 10
- local HIT_DELAY = 0.25
- --------------------------------------------------
- -- ANIMATOR
- --------------------------------------------------
- local animator = humanoid:FindFirstChildOfClass("Animator")
- if not animator then
- animator = Instance.new("Animator")
- animator.Parent = humanoid
- end
- --------------------------------------------------
- -- ПОИСК АНИМАЦИИ АТАКИ
- --------------------------------------------------
- local animateScript = pirate:FindFirstChild("Animate")
- local attackAnimation = nil
- local function searchAttackAnimation()
- if not animateScript then
- return nil
- end
- -- Ищем объект toolslash
- local toolSlash = animateScript:FindFirstChild("toolslash")
- if toolSlash then
- -- Ищем Animation внутри toolslash
- for _, object in ipairs(toolSlash:GetDescendants()) do
- if object:IsA("Animation") then
- return object
- end
- end
- end
- -- Ищем Animation с подходящим названием
- for _, object in ipairs(animateScript:GetDescendants()) do
- if object:IsA("Animation") then
- local name = object.Name:lower()
- if string.find(name, "slash")
- or string.find(name, "attack") then
- return object
- end
- end
- end
- return nil
- end
- attackAnimation = searchAttackAnimation()
- --------------------------------------------------
- -- ЗАГРУЖАЕМ АТАКУ
- --------------------------------------------------
- local attackTrack = nil
- if attackAnimation then
- attackTrack = animator:LoadAnimation(attackAnimation)
- attackTrack.Priority = Enum.AnimationPriority.Action
- attackTrack.Looped = false
- print("Анимация атаки найдена у", pirate.Name)
- print("AnimationId:", attackAnimation.AnimationId)
- else
- warn("У Pirate не найдена стандартная R6-анимация атаки!")
- end
- --------------------------------------------------
- -- ЗВУК МЕЧА
- --------------------------------------------------
- local slashSound = nil
- local handle = sword:FindFirstChild("Handle")
- if handle then
- slashSound = handle:FindFirstChild("SwordSlash")
- end
- --------------------------------------------------
- -- АТАКА
- --------------------------------------------------
- local function attack()
- local player = playerValue.Value
- if not player then
- return
- end
- local character = player.Character
- if not character then
- return
- end
- local targetHumanoid = character:FindFirstChildOfClass("Humanoid")
- local targetRoot = character:FindFirstChild("HumanoidRootPart")
- if not targetHumanoid or not targetRoot then
- return
- end
- if targetHumanoid.Health <= 0 then
- return
- end
- local distance = (
- targetRoot.Position - rootPart.Position
- ).Magnitude
- if distance > ATTACK_DISTANCE then
- return
- end
- --------------------------------------------------
- -- НАЧИНАЕМ АТАКУ
- --------------------------------------------------
- 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(0.05)
- end
- --------------------------------------------------
- -- ЗВУК
- --------------------------------------------------
- if slashSound then
- slashSound:Play()
- end
- --------------------------------------------------
- -- МОМЕНТ ПОПАДАНИЯ
- --------------------------------------------------
- task.wait(HIT_DELAY)
- -- Проверяем игрока ещё раз
- character = player.Character
- if character then
- targetHumanoid = character:FindFirstChildOfClass("Humanoid")
- targetRoot = character:FindFirstChild("HumanoidRootPart")
- if targetHumanoid and targetRoot and targetHumanoid.Health > 0 then
- local newDistance = (
- targetRoot.Position - rootPart.Position
- ).Magnitude
- if newDistance <= ATTACK_DISTANCE then
- targetHumanoid:TakeDamage(DAMAGE)
- end
- end
- end
- --------------------------------------------------
- -- ЖДЁМ КОНЕЦ АНИМАЦИИ
- --------------------------------------------------
- if attackTrack then
- local length = attackTrack.Length
- if length > 0 then
- task.wait(math.max(length - HIT_DELAY, 0))
- else
- task.wait(0.4)
- end
- else
- task.wait(0.4)
- end
- pirate:SetAttribute("IsAttacking", false)
- 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 then
- task.wait(0.2)
- continue
- end
- if targetHumanoid.Health <= 0 then
- task.wait(0.2)
- continue
- end
- local distance = (
- targetRoot.Position - rootPart.Position
- ).Magnitude
- if distance <= ATTACK_DISTANCE then
- attack()
- --------------------------------------------------
- -- СКОРОСТЬ АТАКИ
- --------------------------------------------------
- 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