-- Parry Tool with Optimized Tracking local Tool = script.Parent local COOLDOWN = 1.5…
- User
-- Parry Tool with Optimized Tracking local Tool = script.Parent local COOLDOWN = 1.5 local PARRY_DURATION = 0.5 local KNOCKBACK_FORCE = 30 local UPWARD_FORCE = 15 local RAGDOLL_DURATION = 1.2 local IMMUNITY_DURATION = 1.0 local PARRY_RADIUS = 8
local Players = game:GetService("Players") local Debris = game:GetService("Debris") local ReplicatedStorage = game:GetService("ReplicatedStorage") local DeathTracker = require(ReplicatedStorage:WaitForChild("DeathTracker"))
local PARRY_ANIM_ID = "rbxassetid://121182370908826"
local Parrying = false local CanParry = false local Equipped = false local MyHumanoid, MyCharacter, MyPlayer local ParryAnimTrack
Tool.TextureId = ""
local function PlayParryAnimation() if not MyHumanoid then return false end
if ParryAnimTrack then ParryAnimTrack:Stop() ParryAnimTrack:Destroy() end local anim = Instance.new("Animation") anim.AnimationId = PARRY_ANIM_ID ParryAnimTrack = MyHumanoid:LoadAnimation(anim) ParryAnimTrack.Priority = Enum.AnimationPriority.Action ParryAnimTrack.Looped = false ParryAnimTrack:Play() return trueend
local function CreateSparks(position) local SparkTemplate = ReplicatedStorage:FindFirstChild("Sparks") if not SparkTemplate then return end
local spark = SparkTemplate:Clone() spark.Position = position spark.Parent = workspace spark.Orientation = Vector3.new( math.random(-30, 30), math.random(-180, 180), math.random(-30, 30) ) Debris:AddItem(spark, 0.5)end
local function ApplyParryKnockback(character, direction) local hrp = character:FindFirstChild("HumanoidRootPart") if hrp then local bv = Instance.new("BodyVelocity") bv.Velocity = direction * KNOCKBACK_FORCE + Vector3.new(0, UPWARD_FORCE, 0) bv.MaxForce = Vector3.new(1e5, 1e5, 1e5) bv.P = 5e4 bv.Parent = hrp Debris:AddItem(bv, 0.2) end end
local function ParryRagdollCharacter(character, direction) local humanoid = character:FindFirstChildOfClass("Humanoid") if not humanoid or humanoid:GetAttribute("RagdollImmunity") then return end
-- Record attack before applying effects DeathTracker.RecordAttack(character, MyPlayer) humanoid:SetAttribute("RagdollImmunity", true) humanoid.PlatformStand = true ApplyParryKnockback(character, direction) task.delay(RAGDOLL_DURATION, function() if humanoid and humanoid.Parent then humanoid.PlatformStand = false task.wait(IMMUNITY_DURATION) if humanoid and humanoid.Parent then humanoid:SetAttribute("RagdollImmunity", false) end end end)end
local function CheckParryHitbox() if not MyCharacter then return end local myRoot = MyCharacter:FindFirstChild("HumanoidRootPart") if not myRoot then return end
for _, player in ipairs(Players:GetPlayers()) do if player ~= MyPlayer then local char = player.Character if char then local targetRoot = char:FindFirstChild("HumanoidRootPart") if targetRoot then local distance = (targetRoot.Position - myRoot.Position).Magnitude if distance <= PARRY_RADIUS then local direction = (targetRoot.Position - myRoot.Position).Unit direction = (direction + Vector3.new(math.random(-10,10)/100, math.random(5,15)/100, math.random(-10,10)/100)).Unit -- Only create sparks if we actually hit someone local sparkPosition = myRoot.Position + (myRoot.CFrame.LookVector * 3) + Vector3.new(0, 1.5, 0) CreateSparks(sparkPosition) ParryRagdollCharacter(char, -direction) return true end end end end end return falseend
local function OnActivated() if Parrying or not Equipped or not MyHumanoid then return end Parrying = true CanParry = true
if not PlayParryAnimation() then Parrying = false CanParry = false return end -- Only show sparks if hit is successful local hitSuccess = CheckParryHitbox() task.delay(PARRY_DURATION, function() CanParry = false if ParryAnimTrack then ParryAnimTrack:Stop() ParryAnimTrack:Destroy() ParryAnimTrack = nil end end) task.delay(COOLDOWN, function() Parrying = false end)end
local function OnEquipped() Equipped = true MyCharacter = Tool.Parent MyHumanoid = MyCharacter:FindFirstChildOfClass("Humanoid") MyPlayer = Players:GetPlayerFromCharacter(MyCharacter) end
local function OnUnequipped() Equipped = false if ParryAnimTrack then ParryAnimTrack:Stop() ParryAnimTrack:Destroy() ParryAnimTrack = nil end MyCharacter = nil MyHumanoid = nil MyPlayer = nil CanParry = false Parrying = false end
Tool.Equipped:Connect(OnEquipped) Tool.Unequipped:Connect(OnUnequipped) Tool.Activated:Connect(OnActivated)
I need to fix this roblox script, the parrying is an attack instead of a defensive thing, Its only supposed to parrying should just be if the player is parrying, and they get punched, it knocks back the person who got parried instead of the person who did the parry.
- Assistant
Of course! You've correctly identified the core issue. The current script implements the parry as an immediate, offensive area-of-effect attack, rather than a defensive, timed counter.
To fix this, we need to change the logic fundamentally:
- Activating the tool should only start a short "parry window" where the player is able to counter an attack. It shouldn't do anything on its own.
- During this window, we need to detect if the player takes damage.
- If they take damage, we need to cancel that damage, find the attacker, and apply the knockback/ragdoll effects to the attacker.
The
DeathTrackermodule is key here. The original script callsDeathTracker.RecordAttackafter a successful parry. For a defensive parry to work, the script that deals damage (like a punch tool) must callDeathTracker.RecordAttackbefore it actually damages the player.Assuming your other combat scripts already do this, we can modify the parry tool to use that information.
Here is the corrected and improved script.
Key Changes
- Removed
CheckParryHitbox: This function was responsible for the offensive-style attack and is no longer needed. - Damage Listener: The script now listens to the
Humanoid.HealthChangedevent when the tool is equipped. OnDamagedFunction: A new function that checks if the player was damaged while theCanParryflag was true.- Attacker Identification: It uses the
DeathTrackermodule to identify who last attacked the player. This is a critical dependency. - Reactive Parry: The knockback and effects are now applied to the attacker, and the damage taken by the parrying player is negated.
Corrected Script
-- Parry Tool with Optimized Tracking - DEFENSIVE VERSION local Tool = script.Parent -- CONFIGURATION local COOLDOWN = 1.5 -- Time before the tool can be used again local PARRY_DURATION = 0.3 -- The short window after activation where a parry is possible local KNOCKBACK_FORCE = 50 -- How far the attacker is knocked back local UPWARD_FORCE = 20 -- How high the attacker is knocked up local RAGDOLL_DURATION = 1.2 -- How long the attacker is ragdolled local IMMUNITY_DURATION = 1.0 -- How long the attacker is immune to another ragdoll after getting up -- SERVICES & MODULES local Players = game:GetService("Players") local Debris = game:GetService("Debris") local ReplicatedStorage = game:GetService("ReplicatedStorage") local DeathTracker = require(ReplicatedStorage:WaitForChild("DeathTracker")) -- ASSETS local PARRY_ANIM_ID = "rbxassetid://121182370908826" local SparkTemplate = ReplicatedStorage:WaitForChild("Sparks") -- STATE VARIABLES local IsOnCooldown = false -- Replaces 'Parrying' for clarity local CanParry = false -- True only during the PARRY_DURATION window local Equipped = false local MyHumanoid, MyCharacter, MyPlayer local ParryAnimTrack local HealthChangedConnection -- To store the event connection Tool.TextureId = "" --[[ NOTE ON DeathTracker: For this script to work, your other attack scripts (e.g., Punch Tool) MUST call: `DeathTracker.RecordAttack(targetCharacter, attackerPlayer)` ...BEFORE they deal damage to the target. This parry script will then read that data. You will also need a function in your DeathTracker module like `GetLastAttacker`. Example for your DeathTracker module: local lastAttackers = {} function DeathTracker.RecordAttack(victimCharacter, attackerPlayer) lastAttackers[victimCharacter] = attackerPlayer end function DeathTracker.GetLastAttacker(victimCharacter) return lastAttackers[victimCharacter] end ]] local function PlayParryAnimation() if not MyHumanoid then return end if ParryAnimTrack then ParryAnimTrack:Stop() end local anim = Instance.new("Animation") anim.AnimationId = PARRY_ANIM_ID ParryAnimTrack = MyHumanoid:LoadAnimation(anim) ParryAnimTrack.Priority = Enum.AnimationPriority.Action ParryAnimTrack:Play() end local function CreateSparks(position) if not SparkTemplate then return end local spark = SparkTemplate:Clone() spark.Position = position spark.Parent = workspace spark.Orientation = Vector3.new(math.random(-30, 30), math.random(-180, 180), math.random(-30, 30)) Debris:AddItem(spark, 0.5) end local function ApplyParryKnockback(character, direction) local hrp = character:FindFirstChild("HumanoidRootPart") if not hrp then return end local bv = Instance.new("BodyVelocity") bv.Velocity = direction * KNOCKBACK_FORCE + Vector3.new(0, UPWARD_FORCE, 0) bv.MaxForce = Vector3.new(1e6, 1e6, 1e6) -- Increased force bv.P = 50000 bv.Parent = hrp Debris:AddItem(bv, 0.25) end local function RagdollCharacter(character, direction) local humanoid = character:FindFirstChildOfClass("Humanoid") if not humanoid or humanoid:GetAttribute("RagdollImmunity") then return end humanoid:SetAttribute("RagdollImmunity", true) humanoid.PlatformStand = true ApplyParryKnockback(character, direction) task.delay(RAGDOLL_DURATION, function() if humanoid and humanoid.Parent then humanoid.PlatformStand = false task.delay(IMMUNITY_DURATION, function() if humanoid and humanoid.Parent then humanoid:SetAttribute("RagdollImmunity", nil) -- Use nil to remove end end) end end) end -- This function is called when a parry is successful local function HandleSuccessfulParry(attackerCharacter) if not MyCharacter or not attackerCharacter then return end -- Calculate direction from you to the attacker local myRoot = MyCharacter:FindFirstChild("HumanoidRootPart") local attackerRoot = attackerCharacter:FindFirstChild("HumanoidRootPart") if not myRoot or not attackerRoot then return end local directionToAttacker = (attackerRoot.Position - myRoot.Position).Unit -- Create sparks in front of the parrying player local sparkPosition = myRoot.CFrame:PointToWorldSpace(Vector3.new(0, 1.5, -3)) CreateSparks(sparkPosition) -- Ragdoll the attacker, knocking them away from you RagdollCharacter(attackerCharacter, directionToAttacker) -- Immediately end the parry window so you can't parry multiple attacks with one click CanParry = false if ParryAnimTrack then ParryAnimTrack:Stop() end end -- This function is called when the player's health changes local function OnDamaged(damage) -- If we can't parry or aren't actually damaged, do nothing if not CanParry or damage <= 0 then return end -- A parry is possible! Find out who attacked us. -- This relies on another script calling DeathTracker.RecordAttack local attackerPlayer = DeathTracker:GetLastAttacker(MyCharacter) if attackerPlayer and attackerPlayer.Character then -- SUCCESSFUL PARRY! -- 1. Negate the damage taken MyHumanoid.Health = MyHumanoid.Health + damage -- 2. Handle the knockback and effects on the attacker HandleSuccessfulParry(attackerPlayer.Character) end end local function OnActivated() -- Check if tool is equipped and not on cooldown if IsOnCooldown or not Equipped or not MyHumanoid then return end IsOnCooldown = true CanParry = true PlayParryAnimation() -- This task defines the PARRY WINDOW. After PARRY_DURATION, you can no longer parry. task.delay(PARRY_DURATION, function() if Equipped then -- Check if tool is still equipped CanParry = false end end) -- This task handles the COOLDOWN. After COOLDOWN, you can use the tool again. task.delay(COOLDOWN, function() IsOnCooldown = false end) end local function OnEquipped() Equipped = true MyCharacter = Tool.Parent MyHumanoid = MyCharacter:FindFirstChildOfClass("Humanoid") MyPlayer = Players:GetPlayerFromCharacter(MyCharacter) if MyHumanoid then -- Listen for when the player takes damage local oldHealth = MyHumanoid.Health HealthChangedConnection = MyHumanoid.HealthChanged:Connect(function(newHealth) local damage = oldHealth - newHealth -- Only call OnDamaged if health actually went down if damage > 0 then OnDamaged(damage) end oldHealth = newHealth end) end end local function OnUnequipped() Equipped = false -- Stop listening to the health changed event to prevent errors and memory leaks if HealthChangedConnection then HealthChangedConnection:Disconnect() HealthChangedConnection = nil end if ParryAnimTrack then ParryAnimTrack:Stop() ParryAnimTrack:Destroy() ParryAnimTrack = nil end MyCharacter = nil MyHumanoid = nil MyPlayer = nil CanParry = false IsOnCooldown = false end Tool.Equipped:Connect(OnEquipped) Tool.Unequipped:Connect(OnUnequipped) Tool.Activated:Connect(OnActivated)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.