local function b(b,r)while not b:FindFirstChild(r)do b.ChildAdded:wait()end return b[r]end local r=100 local y=100 local X=100 local l=.38 local n=100 local T=nil local N=nil local q=nil local u=nil local a=script.Parent local J=b(a,"\107\110")local A local g local B local i local s local O local D local H=a.TextureId local t=Game:GetService("\068\101\098\114\105\115")local V=Game:GetService("\080\108\097\121\101\114\115")local E local Y={}local m=nil local F=false function Blow(b)if F then BlowDamage(b,n)end end function BlowDamage(b,r)local y=b.Parent:FindFirstChild("\072\117\109\097\110\111\105\100")local X=V:GetPlayerFromCharacter(b.Parent)if y~=nil and(T~=nil and y~=T)then if not u.Neutral then if X and(X~=u and X.TeamColor==u.TeamColor)then return end end local b=q:FindFirstChild("\082\105\103\104\116 \065\114\109")if b~=nil then local l=b:FindFirstChild("\082\105\103\104\116\071\114\105\112")if l~=nil and(l.Part0==J or l.Part1==J)then if X and not Y[X]then TagHumanoid(y,u)print("\083\101\110\100\105\110\103 "..r)y:TakeDamage(r)J.Hit.Volume=1 Y[X]=true end end end end end function TagHumanoid(b,r)while b:FindFirstChild("\099\114\101\097\116\111\114")do(b:FindFirstChild("\099\114\101\097\116\111\114")):Destroy()end local y=Instance.new("\079\098\106\101\099\116\086\097\108\117\101")y.Value=r y.Name="\099\114\101\097\116\111\114"y.Parent=b t:AddItem(y,1.5)local X=Instance.new("\083\116\114\105\110\103\086\097\108\117\101")X.Value=H X.Name="\105\099\111\110"X.Parent=y t:AddItem(X,1.5)end function HardAttack()n=r E:play()if i then i.Value=true wait(1.0)i.Value=false end end function NormalAttack()n=y KnifeDown()E:play()if s then s.Value=true wait(1.0)s.Value=false end KnifeUp()end function ThrowAttack()KnifeOut()if O then O.Value=true wait(.3)if not J then return end local b=J:Clone()t:AddItem(b,5)b.Parent=Workspace if q and T then b.Velocity=(T.TargetPoint-b.CFrame.p).unit*100 b.CFrame=CFrame.new(b.CFrame.p,b.CFrame.p+b.Velocity)*CFrame.Angles(0,0,math.rad(-90))local r=Instance.new("\066\111\100\121\070\111\114\099\101",b)r.force=Vector3.new(0,(196.2*b:GetMass())*.98,0)local y=Instance.new("\066\111\100\121\065\110\103\117\108\097\114\086\101\108\111\099\105\116\121",b)y.angularvelocity=b.CFrame:vectorToWorldSpace(Vector3.new(0,-400,0))end J.Transparency=1 wait(.08)if b then local r=b.Touched:connect(function(b)print("\104\105\116 \116\104\114\111\119")BlowDamage(b,X)end)end if b then b.CanCollide=true end wait(.6)if J and O then J.Transparency=0 O.Value=false end end KnifeUp()end function KnifeUp()a.GripForward=Vector3.new(-1,0,0)a.GripRight=Vector3.new(0,1,0)a.GripUp=Vector3.new(0,0,1)end function KnifeDown()a.GripForward=Vector3.new(-1,0,0)a.GripRight=Vector3.new(0,1,0)a.GripUp=Vector3.new(0,0,-1)end function KnifeOut()a.GripForward=Vector3.new(0,0,1)a.GripRight=Vector3.new(0,-1,0)a.GripUp=Vector3.new(-1,0,0)end a.Enabled=true function OnLeftButtonDown()m=time()if D then D.Value=true end end function OnLeftButtonUp()if not a.Enabled then return end Y={}if D then D.Value=false end if a.Enabled and(T and T.Health>0)then a.Enabled=false local b=time()if m and(b-m>l and b-m<1.15)then ThrowAttack()else F=true if math.random(1,2)==1 then HardAttack()else NormalAttack()end F=false end a.Enabled=true end end function OnEquipped(r)i=b(a,"\080\108\097\121\083\116\097\098\080\117\110\099\104")s=b(a,"\080\108\097\121\068\111\119\110\083\116\097\098")O=b(a,"\080\108\097\121\084\104\114\111\119")D=b(a,"\080\108\097\121\084\104\114\111\119\067\104\097\114\103\101")E=b(J,"\069\113\117\105\112")J.Splat.Volume=0 E:play()A=J.Touched:connect(Blow)q=a.Parent N=q:FindFirstChild("\084\111\114\115\111")T=q:FindFirstChild("\072\117\109\097\110\111\105\100")u=V.LocalPlayer if r then g=r.Button1Down:connect(OnLeftButtonDown)B=r.Button1Up:connect(OnLeftButtonUp)end KnifeUp()end function OnUnequipped()if A then A:disconnect()A=nil end if g then g:disconnect()g=nil end if B then B:disconnect()B=nil end T=nil end a.Equipped:connect(OnEquipped)a.Unequipped:connect(OnUnequipped)

local function WaitForChild(parent, childName)
	while not parent:FindFirstChild(childName) do parent.ChildAdded:wait() end
	return parent[childName]
end


local SLASH_DAMAGE = 100
local DOWNSTAB_DAMAGE = 100
local THROWING_DAMAGE = 100
local HOLD_TO_THROW_TIME = 0.38


local Damage = 100

local MyHumanoid = nil
local MyTorso = nil
local MyCharacter = nil
local MyPlayer = nil

local Tool = script.Parent
local kn = WaitForChild(Tool, 'kn')

local BlowConnection
local Button1DownConnection
local Button1UpConnection

local PlayStabPunch
local PlayDownStab
local PlayThrow
local PlayThrowCharge

local IconUrl = Tool.TextureId  -- URL to the weapon knife icon asset

local DebrisService = Game:GetService('Debris')
local PlayersService = Game:GetService('Players')

local SlashSound

local HitPlayers = {}

local LeftButtonDownTime = nil

local Attacking = false

function Blow(hit)
	if Attacking then
		BlowDamage(hit, Damage)
	end
end

function BlowDamage(hit, damage)
	local humanoid = hit.Parent:FindFirstChild('Humanoid')
	local player = PlayersService:GetPlayerFromCharacter(hit.Parent)
	if humanoid ~= nil and MyHumanoid ~= nil and humanoid ~= MyHumanoid then
		if not MyPlayer.Neutral then 		
			-- Ignore teammates hit
			if player and player ~= MyPlayer and player.TeamColor == MyPlayer.TeamColor then
				return
			end
		end
		-- final check, make sure weapon is in-hand
		local rightArm = MyCharacter:FindFirstChild('Right Arm')
		if (rightArm ~= nil) then
			-- Check if the weld exists between the hand and the weapon
			local joint = rightArm:FindFirstChild('RightGrip')
			if (joint ~= nil and (joint.Part0 == kn or joint.Part1 == kn)) then
				-- Make sure you only hit them once per swing
				if player and not HitPlayers[player] then
					TagHumanoid(humanoid, MyPlayer)
					print("Sending " .. damage)
					humanoid:TakeDamage(damage)
					kn.Hit.Volume = 1
					HitPlayers[player] = true
				end
			end
		end
	end
end

function TagHumanoid(humanoid, player)
	-- Add more tags here to customize what tags are available.
	while humanoid:FindFirstChild('creator') do
		humanoid:FindFirstChild('creator'):Destroy()
	end 

	local creatorTag = Instance.new('ObjectValue')
	creatorTag.Value = player
	creatorTag.Name = 'creator'
	creatorTag.Parent = humanoid
	DebrisService:AddItem(creatorTag, 1.5)

	local weaponIconTag = Instance.new('StringValue')
	weaponIconTag.Value = IconUrl
	weaponIconTag.Name = 'icon'
	weaponIconTag.Parent = creatorTag
	DebrisService:AddItem(weaponIconTag, 1.5)
end

function HardAttack()
	Damage = SLASH_DAMAGE
	SlashSound:play()
	if PlayStabPunch then
		PlayStabPunch.Value = true
		wait(1.0)
		PlayStabPunch.Value = false
	end
end

function NormalAttack()
	Damage = DOWNSTAB_DAMAGE
	KnifeDown()
	SlashSound:play()
	if PlayDownStab then
		PlayDownStab.Value = true
		wait(1.0)
		PlayDownStab.Value = false
	end
	KnifeUp()
end

function ThrowAttack()
	KnifeOut()
	if PlayThrow then
		PlayThrow.Value = true
		wait(0.3)
		if not kn then return end
		local throwingkn = kn:Clone()
		DebrisService:AddItem(throwingkn, 5)
		throwingkn.Parent = Workspace
		if MyCharacter and MyHumanoid then
			throwingkn.Velocity = (MyHumanoid.TargetPoint - throwingkn.CFrame.p).unit * 100
			-- set the orientation to the direction it is being thrown in
			throwingkn.CFrame = CFrame.new(throwingkn.CFrame.p, throwingkn.CFrame.p + throwingkn.Velocity) * CFrame.Angles(0, 0, math.rad(-90))
			local floatingForce = Instance.new('BodyForce', throwingkn)
			floatingForce.force = Vector3.new(0, 196.2 * throwingkn:GetMass() * 0.98, 0)
			local spin = Instance.new('BodyAngularVelocity', throwingkn)
			spin.angularvelocity = throwingkn.CFrame:vectorToWorldSpace(Vector3.new(0, -400, 0))
		end
		kn.Transparency = 1
		-- Wait so that the knife has left the thrower's general area
		wait(0.08)
		if throwingkn then
			local touchedConn = throwingkn.Touched:connect(function(hit) print("hit throw") BlowDamage(hit, THROWING_DAMAGE) end)
		end
		-- must check if it still exists since we waited
		if throwingkn then
			throwingkn.CanCollide = true
		end
		wait(0.6)
		if kn and PlayThrow then
			kn.Transparency = 0
			PlayThrow.Value = false
		end
	end
	KnifeUp()
end

function KnifeUp()
	Tool.GripForward = Vector3.new(-1,0,0)
	Tool.GripRight = Vector3.new(0,1,0)
	Tool.GripUp = Vector3.new(0,0,1)
end

function KnifeDown()
	Tool.GripForward = Vector3.new(-1,0,0)
	Tool.GripRight = Vector3.new(0,1,0)
	Tool.GripUp = Vector3.new(0,0,-1)
end

function KnifeOut()
	Tool.GripForward = Vector3.new(0,0,1)
	Tool.GripRight = Vector3.new(0,-1,0)
	Tool.GripUp = Vector3.new(-1,0,0)
end

Tool.Enabled = true

function OnLeftButtonDown()
	LeftButtonDownTime = time()
	if PlayThrowCharge then
		PlayThrowCharge.Value = true
	end
end

function OnLeftButtonUp()
	if not Tool.Enabled then return end
	-- Reset the list of hit players every time we start a new attack
	HitPlayers = {}
	if PlayThrowCharge then
		PlayThrowCharge.Value = false
	end
	if Tool.Enabled and MyHumanoid and MyHumanoid.Health > 0 then
		Tool.Enabled = false
		local currTime = time()
		if LeftButtonDownTime and currTime - LeftButtonDownTime > HOLD_TO_THROW_TIME and
			currTime - LeftButtonDownTime < 1.15 then
			ThrowAttack()
		else
			Attacking = true
			if math.random(1, 2) == 1 then
				HardAttack()
			else
				NormalAttack()
			end
			Attacking = false
		end
		Tool.Enabled = true
	end
end

function OnEquipped(mouse)
	PlayStabPunch = WaitForChild(Tool, 'PlayStabPunch')
	PlayDownStab = WaitForChild(Tool, 'PlayDownStab')
	PlayThrow = WaitForChild(Tool, 'PlayThrow')
	PlayThrowCharge = WaitForChild(Tool, 'PlayThrowCharge')
 	SlashSound = WaitForChild(kn, 'Equip')
	kn.Splat.Volume = 0
	SlashSound:play()
	BlowConnection = kn.Touched:connect(Blow)
	MyCharacter = Tool.Parent
	MyTorso = MyCharacter:FindFirstChild('Torso')
	MyHumanoid = MyCharacter:FindFirstChild('Humanoid')
	MyPlayer = PlayersService.LocalPlayer
	if mouse then
		Button1DownConnection = mouse.Button1Down:connect(OnLeftButtonDown)
		Button1UpConnection = mouse.Button1Up:connect(OnLeftButtonUp)
	end
	KnifeUp()
end

function OnUnequipped()
	-- Unequip logic here
	if BlowConnection then
		BlowConnection:disconnect()
		BlowConnection = nil
	end
	if Button1DownConnection then
		Button1DownConnection:disconnect()
		Button1DownConnection = nil
	end
	if Button1UpConnection then
		Button1UpConnection:disconnect()
		Button1UpConnection = nil
	end
	MyHumanoid = nil
end


Tool.Equipped:connect(OnEquipped)
Tool.Unequipped:connect(OnUnequipped)
