-- 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 true
end

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 false
end

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.