--[[ LawlR fix ]]
--

local ds = game:GetService("Debris")
local ps = game:GetService("Players")

wait(1)

local tool = script.Parent
local handle = tool:WaitForChild("Handle")
local slashSound = handle.Slash
local lungeSound = handle.Lunge
local unsheathSound = handle.Unsheath

local baseDmg = 5
local slashDmg = 10
local lungeDmg = 20
local dmg = slashDmg
local gripUp = CFrame.new(0, 0, -2.35, 0, 0, 1, 1, 0, 0, 0, 1, 0)
local gripOut = CFrame.new(0, 0, -2.35, 0, 1, 0, 1, -0, 0, 0, 0, -1)
local lastAttack = 0

local plr, chr, hum
local db
local lastAttackTag

local attackAnimString = Instance.new("StringValue")
attackAnimString.Name = "toolanim"


local function tagHum(hum)
	local tag = Instance.new("ObjectValue")
	tag.Name = "creator"
	tag.Value = plr
	tag.Parent = hum
	ds:AddItem(tag, 2)
end

local function isTeamMate(hitPlr)
	return (hitPlr and plr and not hitPlr.Neutral and not plr.Neutral and hitPlr.TeamColor == plr.TeamColor)
end

local function onHit(hit)
	local hitHum = hit.Parent:FindFirstChildWhichIsA("Humanoid") or hit.Parent.Parent:FindFirstChildWhichIsA("Humanoid")
	if hitHum and hitHum.Health > 0 then
		if hitHum ~= hum then
			local hitChr = hitHum.Parent
			local hitPlr = ps:GetPlayerFromCharacter(hitChr)
			if hitPlr and isTeamMate(hitPlr) then
				return
			end
			tagHum(hitHum)
			hitHum:TakeDamage(dmg)
		end
	end
end

local function attack(tag)
	dmg = slashDmg
	slashSound:play()
	
	local anim = attackAnimString:Clone()
	anim.Value = "Slash"
	anim.Parent = tool
	
	coroutine.wrap(function()
		wait(.5)
		if lastAttackTag == tag then
			dmg = baseDmg
		end
	end)()
end

local function lunge()
	dmg = lungeDmg
	lungeSound:play()

	local anim = attackAnimString:Clone()
	anim.Value = "Lunge"
	anim.Parent = tool
	
	wait(.2)
	tool.Grip = gripOut
	wait(.6)
	tool.Grip = gripUp
	dmg = baseDmg
end


tool.Activated:Connect(function()
	if not db then
		db = true
		tool.Enabled = false
		local t = tick()
		lastAttackTag = t
	
		if (t - lastAttack < .2) then
			lunge()
		else
			attack(t)
		end
	
		lastAttack = t
		db = false
		tool.Enabled = true
	end
end)

tool.Equipped:Connect(function()
	unsheathSound:Play()
	lastAttack = tick()
	local tempChr = tool.Parent
	local tempHum = tempChr:FindFirstChildWhichIsA("Humanoid")
	if tempHum then
		chr = tempChr
		hum = tempHum
		plr = ps:GetPlayerFromCharacter(tempChr)
	end
end)


handle.Touched:Connect(function(hit)
	local toolParent = tool.Parent
	if toolParent and toolParent:IsA("Model") and toolParent ~= workspace then
		onHit(hit)
	end
end)
