--local pathfinder = require(game.ServerScriptService.Pathfinder)

K9 = script.Parent.Parent.K9.Value
a1 = Instance.new("Attachment")
a1.Parent = K9.Callor
a2 = Instance.new("Attachment")
a2.Parent = adminchar["Left Arm"]
rope = Instance.new("RopeConstraint")
rope.Parent = K9.Badge
rope.Enabled = false
rope.Attachment0 = a2
rope.Attachment1 = a1
rope.Length = 7
rope.Color = BrickColor.new("Really black")
rope.Thickness = 0.07
a2.Position = Vector3.new(0.04, -1, 0.01)


local plr = script.Parent.Parent.Player.Value
plr.CharacterRemoving:connect(function()
	script.Parent.Parent.K9.Value:Destroy()
end)

script.Parent.Teleport.MouseButton1Click:connect(function ()
	following = false roaming = false attacking = false tracking = false K9.Humanoid.Sit = false wait(.1)
	K9:MoveTo((adminchar.Torso.CFrame * CFrame.new(0,0,5)).p, adminchar.Torso)
end)


function Stay()
	following = false roaming = false attacking = false tracking = false K9.Humanoid.Sit = false wait(.1)
	K9.Humanoid:MoveTo(K9.Torso.Position, adminchar.Torso)
end
script.Parent.Stay.MouseButton1Click:connect(Stay)


function Follow()
	following = false attacking = false tracking = false K9.Humanoid.Jump = false K9.Humanoid.Sit = false
	following = true
	repeat wait()
		K9.Humanoid.WalkSpeed = adminchar.Humanoid.WalkSpeed
		K9.Humanoid:MoveTo((adminchar.Torso.CFrame * CFrame.new(0,0,5)).p, adminchar.Torso)
	until not following
	following = false
end
script.Parent.Come.MouseButton1Click:connect(Follow)


function SickEm()
	local Guibox = script.Parent.TargetLabel.Text
	plr = game.Players[Guibox]
	following = false roaming = false attacking = false tracking = false K9.Humanoid.Jump = false K9.Humanoid.Sit = false
	if plr and plr.Character and plr.Character:findFirstChild("Torso") and plr.Character:findFirstChild("Humanoid") then
		attacking = true
		local conn = K9.Mouth.Touched:connect(function(hit) if attacking == true and hit.Parent == plr.Character then repeat plr.Character.Humanoid.Health = plr.Character.Humanoid.Health -15 plr.Character.Humanoid.Sit = true Bark() plr.Character.Humanoid.WalkSpeed = 6 until plr.Character.Humanoid.Health <= 0 attacking = false  Bark() Stay()  end end)
		repeat wait()
			K9.Humanoid.WalkSpeed = plr.Character.Humanoid.WalkSpeed + 20
			K9.Humanoid:MoveTo(plr.Character.Torso.Position, plr.Character.Torso)
		until not attacking
		plr.Character.Humanoid.WalkSpeed = 12
		attacking = false
		conn:disconnect()
	end
end
script.Parent.Kill.MouseButton1Click:connect(SickEm)

function Attack()
	local Guibox = script.Parent.TargetLabel.Text
	plr = game.Players[Guibox]
	following = false roaming = false attacking = false tracking = false K9.Humanoid.Jump = false K9.Humanoid.Sit = false
	if plr and plr.Character and plr.Character:findFirstChild("Torso") and plr.Character:findFirstChild("Humanoid") then
		attacking = true
		local conn = K9.Mouth.Touched:connect(function(hit) if attacking == true and hit.Parent == plr.Character then plr.Character.Humanoid.Health = plr.Character.Humanoid.Health -15 plr.Character.Humanoid.Sit = true Bark() plr.Character.Humanoid.WalkSpeed = 6  attacking = false  Bark() Stay()  end end)
		repeat wait()
			K9.Humanoid.WalkSpeed = plr.Character.Humanoid.WalkSpeed + 20
			K9.Humanoid:MoveTo(plr.Character.Torso.Position, plr.Character.Torso)
		until not attacking
		plr.Character.Humanoid.WalkSpeed = 12
		attacking = false
		conn:disconnect()
	end
end
script.Parent.Attack.MouseButton1Click:connect(Attack)


function Bark()
	K9.Head.Bark:Play()
end
script.Parent.Bark.MouseButton1Click:connect(Bark)


function Vest()
	if K9.Vest1.Transparency == 0 then
		VestOff()
		else if K9.Vest1.Transparency == 1 then
		VestOn()
		end
	end
end
script.Parent.ToggleVest.MouseButton1Click:connect(Vest)



function VestOff()
	K9.Vest1.Transparency = 1
	K9.K9NameInd.Transparency = 1
	K9.K9NameInd.Left.Enabled = false
	K9.K9NameInd.Right.Enabled = false
	K9.Credit.Left.Enabled = false
	K9.Credit.Right.Enabled = false
	K9.Badge1.SurfaceGui.Enabled = false
	K9.Badge2.SurfaceGui.Enabled = false
	K9.Humanoid.MaxHealth = 150
	K9.Humanoid.Health = 150
	
end

function VestOn()
	K9.Vest1.Transparency = 0
	K9.K9NameInd.Transparency = 0
	K9.K9NameInd.Left.Enabled = true
	K9.K9NameInd.Right.Enabled = true
	K9.Credit.Left.Enabled = true
	K9.Credit.Right.Enabled = true
	K9.Badge1.SurfaceGui.Enabled = true
	K9.Badge2.SurfaceGui.Enabled = true
	K9.Humanoid.MaxHealth = 200
	K9.Humanoid.Health = 200
	
end

function Track()
	following = false attacking = false tracking = false K9.Humanoid.Jump = true K9.Humanoid.Sit = false
	local Guibox = script.Parent.TargetLabel.Text
	local plrz = game.Players[Guibox]
	if plrz and plrz.Character and plrz.Character:findFirstChild("Torso") and plrz.Character:findFirstChild("Humanoid") then
		following = true
		repeat wait()
			K9.Humanoid.WalkSpeed = plrz.Character.Humanoid.WalkSpeed+5
			K9.Humanoid:MoveTo((plrz.Character.Torso.CFrame * CFrame.new(0,0,5)).p, plrz.Character.Torso)
		until not following
		following = false
	end
end
script.Parent.Track.MouseButton1Click:connect(Track)
script.Parent.FindNearest.MouseButton1Click:connect(Track)

function Leash()
		if rope.Visible == false then
		rope.Visible = true
		rope.Enabled = true	
		Follow()
	else if rope.Visible == true then
		rope.Visible = false
		rope.Enabled = false
		end
	end
end
script.Parent.ToggleLeash.MouseButton1Click:connect(Leash)


function CleanUp()
	for _,v in pairs(script.Parent.Parent.PlayerSelection:GetChildren()) do
		if v.Name ~= "Template" then
			v:Destroy()
		end
	end
end

script.Parent.TargetBox.Changed:connect(function (onChange)
	CleanUp()
	local searchTxt = script.Parent.TargetBox.Text
	local plrs = {}
	for _,player in pairs(game.Players:GetPlayers()) do
		if string.find(string.lower(player.Name), string.lower(searchTxt)) then
			table.insert(plrs, player)
		end
	end
	local PlrSel = script.Parent.Parent.PlayerSelection
	for i = 1, #plrs do
		local tem = PlrSel.Template:Clone()
		tem.Name = plrs[i].Name
		tem.Position = UDim2.new(0, 25, 0, ((i-1)*25)+5)
		tem.Parent = PlrSel
		tem.Text = plrs[i].Name
		tem.Visible = true
		tem.MouseButton1Down:connect(function ()
			script.Parent.TargetLabel.Text = tem.Text
			CleanUp()
		end)
		PlrSel.CanvasSize = UDim2.new(0,0,0,(i*30))
	end
end)


