Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local Players = game:GetService("Players")
- local ServerScriptService = game:GetService("ServerScriptService")
- local tool = script.Parent
- local punchEvent = tool:WaitForChild("PunchEvent")
- local KillTagger = require(
- ServerScriptService:WaitForChild("KillTagger")
- )
- -- НАСТРОЙКИ
- local DAMAGE = 6
- local COOLDOWN = 0.65
- local HITBOX_SIZE = Vector3.new(4.2, 4, 4.5)
- local HITBOX_OFFSET = CFrame.new(0, 0, -2.3)
- local SWING_DURATION = 0.15
- local CHECK_INTERVAL = 0.04
- -- Отбрасывание
- local KNOCKBACK = 38
- local UPWARD_KNOCKBACK = 22
- -- Время падения
- local RAGDOLL_DURATION = 2
- local lastPunch = {}
- local ragdollStates = {}
- --------------------------------------------------
- -- RAGDOLL: ПАДЕНИЕ И ПОТЕРЯ УПРАВЛЕНИЯ
- --------------------------------------------------
- local function startRagdoll(character, humanoid)
- local state = ragdollStates[humanoid]
- -- Если персонаж уже падает, продлеваем эффект
- if state then
- state.version += 1
- else
- state = {
- version = 0,
- attachments = {},
- constraints = {},
- motors = {},
- animations = {},
- oldRequiresNeck = humanoid.RequiresNeck,
- oldPlatformStand = humanoid.PlatformStand,
- oldAutoRotate = humanoid.AutoRotate,
- }
- ragdollStates[humanoid] = state
- local folder = Instance.new("Folder")
- folder.Name = "KnockbackRagdoll"
- folder.Parent = character
- state.folder = folder
- -- Отключаем смерть при временном отсоединении шеи
- humanoid.RequiresNeck = false
- humanoid.AutoRotate = false
- humanoid.PlatformStand = true
- -- Сервер управляет физикой персонажа
- local root = character:FindFirstChild("HumanoidRootPart")
- if root then
- pcall(function()
- root:SetNetworkOwner(nil)
- end)
- end
- -- Поддержка новых R15 AnimationConstraint
- for _, joint in ipairs(character:GetDescendants()) do
- if joint:IsA("AnimationConstraint") then
- local a0 = joint.Attachment0
- local a1 = joint.Attachment1
- local part0 = a0 and a0.Parent
- local part1 = a1 and a1.Parent
- -- Не отключаем соединение с HumanoidRootPart
- if part0 and part1
- and part0.Name ~= "HumanoidRootPart"
- and part1.Name ~= "HumanoidRootPart" then
- table.insert(state.animations, {
- joint = joint,
- oldValue = joint.IsKinematic
- })
- -- Включаем физическое поведение соединения
- joint.IsKinematic = false
- end
- end
- end
- -- Поддержка старых R6 / R15 Motor6D
- for _, joint in ipairs(character:GetDescendants()) do
- if joint:IsA("Motor6D")
- and joint.Part0
- and joint.Part1 then
- if joint.Part0.Name ~= "HumanoidRootPart"
- and joint.Part1.Name ~= "HumanoidRootPart" then
- local attachment0 = Instance.new("Attachment")
- attachment0.CFrame = joint.C0
- attachment0.Parent = joint.Part0
- local attachment1 = Instance.new("Attachment")
- attachment1.CFrame = joint.C1
- attachment1.Parent = joint.Part1
- local socket = Instance.new("BallSocketConstraint")
- socket.Attachment0 = attachment0
- socket.Attachment1 = attachment1
- socket.LimitsEnabled = true
- socket.UpperAngle = 45
- socket.TwistLimitsEnabled = true
- socket.TwistLowerAngle = -45
- socket.TwistUpperAngle = 45
- socket.Parent = folder
- table.insert(state.attachments, attachment0)
- table.insert(state.attachments, attachment1)
- table.insert(state.constraints, socket)
- table.insert(state.motors, {
- joint = joint,
- oldValue = joint.Enabled
- })
- joint.Enabled = false
- end
- end
- end
- end
- -- Каждый новый удар продлевает падение
- local currentVersion = state.version
- task.delay(RAGDOLL_DURATION, function()
- if ragdollStates[humanoid] ~= state then
- return
- end
- if state.version ~= currentVersion then
- return
- end
- -- Удаляем физические соединения
- if state.folder and state.folder.Parent then
- state.folder:Destroy()
- end
- for _, attachment in ipairs(state.attachments) do
- if attachment.Parent then
- attachment:Destroy()
- end
- end
- -- Восстанавливаем старые Motor6D
- for _, data in ipairs(state.motors) do
- if data.joint.Parent then
- data.joint.Enabled = data.oldValue
- end
- end
- -- Восстанавливаем AnimationConstraint
- for _, data in ipairs(state.animations) do
- if data.joint.Parent then
- data.joint.IsKinematic = data.oldValue
- end
- end
- -- Возвращаем управление
- if humanoid.Parent and humanoid.Health > 0 then
- humanoid.RequiresNeck = state.oldRequiresNeck
- humanoid.AutoRotate = state.oldAutoRotate
- humanoid.PlatformStand = state.oldPlatformStand
- if not state.oldPlatformStand then
- humanoid:ChangeState(
- Enum.HumanoidStateType.GettingUp
- )
- end
- local root = humanoid.Parent:FindFirstChild("HumanoidRootPart")
- if root then
- pcall(function()
- root:SetNetworkOwnershipAuto()
- end)
- end
- end
- ragdollStates[humanoid] = nil
- end)
- end
- --------------------------------------------------
- -- ОТБРАСЫВАНИЕ
- --------------------------------------------------
- local function knockback(attackerRoot, targetRoot, targetModel, targetHumanoid)
- if not targetRoot or targetRoot.Anchored then
- return
- end
- -- Сначала переводим персонажа в состояние падения
- startRagdoll(targetModel, targetHumanoid)
- local direction = targetRoot.Position - attackerRoot.Position
- direction = Vector3.new(direction.X, 0, direction.Z)
- if direction.Magnitude < 0.001 then
- direction = attackerRoot.CFrame.LookVector
- direction = Vector3.new(direction.X, 0, direction.Z)
- end
- direction = direction.Unit
- -- Создаём физический импульс
- local velocity = (
- direction * KNOCKBACK
- + Vector3.new(0, UPWARD_KNOCKBACK, 0)
- )
- local impulse = velocity * targetRoot.AssemblyMass
- targetRoot:ApplyImpulse(impulse)
- end
- --------------------------------------------------
- -- ПОИСК ПЕРСОНАЖЕЙ
- --------------------------------------------------
- local function findHumanoid(part)
- local current = part
- while current and current ~= workspace do
- if current:IsA("Model") then
- local humanoid = current:FindFirstChildOfClass("Humanoid")
- if humanoid then
- return current, humanoid
- end
- end
- current = current.Parent
- end
- return nil, nil
- end
- --------------------------------------------------
- -- АТАКА
- --------------------------------------------------
- local function punch(player)
- local character = player.Character
- if not character or tool.Parent ~= character then
- return
- end
- local attackerHumanoid = character:FindFirstChildOfClass("Humanoid")
- local attackerRoot = character:FindFirstChild("HumanoidRootPart")
- if not attackerHumanoid
- or attackerHumanoid.Health <= 0
- or not attackerRoot then
- return
- end
- -- Перезарядка
- local now = os.clock()
- if lastPunch[player] and now - lastPunch[player] < COOLDOWN then
- return
- end
- lastPunch[player] = now
- local overlapParams = OverlapParams.new()
- overlapParams.FilterType = Enum.RaycastFilterType.Exclude
- overlapParams.FilterDescendantsInstances = {character}
- overlapParams.MaxParts = 100
- local alreadyHit = {}
- local finishTime = os.clock() + SWING_DURATION
- repeat
- if tool.Parent ~= character then
- break
- end
- local hitboxCFrame = attackerRoot.CFrame * HITBOX_OFFSET
- local parts = workspace:GetPartBoundsInBox(
- hitboxCFrame,
- HITBOX_SIZE,
- overlapParams
- )
- for _, part in ipairs(parts) do
- local targetModel, targetHumanoid = findHumanoid(part)
- if targetHumanoid
- and targetHumanoid ~= attackerHumanoid
- and targetHumanoid.Health > 0
- and not alreadyHit[targetHumanoid] then
- alreadyHit[targetHumanoid] = true
- local targetRoot =
- targetModel:FindFirstChild("HumanoidRootPart")
- or targetModel:FindFirstChild("Torso")
- or targetModel.PrimaryPart
- if targetRoot then
- -- Регистрируем убийцу
- KillTagger.TagHumanoid(targetHumanoid, player)
- -- Отбрасываем и роняем противника
- knockback(
- attackerRoot,
- targetRoot,
- targetModel,
- targetHumanoid
- )
- -- Наносим урон
- targetHumanoid:TakeDamage(DAMAGE)
- print(
- player.Name ..
- " ударил " ..
- targetModel.Name
- )
- end
- end
- end
- task.wait(CHECK_INTERVAL)
- until os.clock() >= finishTime
- end
- --------------------------------------------------
- -- ПОЛУЧЕНИЕ ЗАПРОСА ОТ ИГРОКА
- --------------------------------------------------
- punchEvent.OnServerEvent:Connect(function(player)
- punch(player)
- end)
- Players.PlayerRemoving:Connect(function(player)
- lastPunch[player] = nil
- end)
Advertisement
Add Comment
Please, Sign In to add comment