local I=game:GetService("\080\108\097\121\101\114\115")local v=game:GetService("\082\117\110\083\101\114\118\105\099\101")local U=game:GetService("\085\115\101\114\073\110\112\117\116\083\101\114\118\105\099\101")local c=script.Parent.Parent.Parent.Parent local e=require(c.Parent.Signal)local J={AvatarSelected=e.new(),AvatarDeselected=e.new(),AvatarHovered=e.new();_AvatarSelecting=nil;_AvatarHovering=nil,_AvatarSelectionHighlight=Instance.new("\072\105\103\104\108\105\103\104\116"),_AvatarSelectionConnection=nil}J._AvatarSelectionHighlight.Parent=I.LocalPlayer:WaitForChild("\080\108\097\121\101\114\071\117\105")J._AvatarSelectionHighlight.FillTransparency=1 J._AvatarSelectionHighlight.OutlineTransparency=.25 function J.Start(c)c:Stop()local e=I.LocalPlayer:GetMouse()c._AvatarSelectionConnection=v.RenderStepped:Connect(function()c._AvatarHovering=nil c._AvatarSelectionHighlight.Adornee=nil local v=e.Target if v then local U=v:FindFirstAncestorOfClass("\077\111\100\101\108")if U then local v=I:GetPlayerFromCharacter(U)if v then c._AvatarHovering=U c._AvatarSelectionHighlight.Adornee=U c.AvatarHovered:Fire(U)end end end end)U.InputEnded:Connect(function(v,U)local J={Enum.UserInputType.MouseButton1;Enum.UserInputType.Touch}if not U and table.find(J,v.UserInputType)then if e.Target then local v=e.Target:FindFirstAncestorOfClass("\077\111\100\101\108")if v then local U=I:GetPlayerFromCharacter(v)if U~=nil and U~=I.LocalPlayer then c._AvatarSelecting=v c.AvatarSelected:Fire(v)return end end end if c._AvatarSelecting then c.AvatarDeselected:Fire(c._AvatarSelecting)end c._AvatarSelecting=nil end end)end function J.Stop(I)if I._AvatarSelectionConnection then I._AvatarSelectionConnection:Disconnect()I._AvatarSelectionConnection=nil end I._AvatarSelectionHighlight.Adornee=nil end return J
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")

local Rorooms = script.Parent.Parent.Parent.Parent
local Signal = require(Rorooms.Parent.Signal)

local AvatarSelector = {
	AvatarSelected = Signal.new(),
	AvatarDeselected = Signal.new(),
	AvatarHovered = Signal.new(),
	_AvatarSelecting = nil,
	_AvatarHovering = nil,
	_AvatarSelectionHighlight = Instance.new("Highlight"),
	_AvatarSelectionConnection = nil,
}

AvatarSelector._AvatarSelectionHighlight.Parent = Players.LocalPlayer:WaitForChild("PlayerGui")
AvatarSelector._AvatarSelectionHighlight.FillTransparency = 1
AvatarSelector._AvatarSelectionHighlight.OutlineTransparency = 0.25

function AvatarSelector:Start()
	self:Stop()

	local Mouse = Players.LocalPlayer:GetMouse()

	self._AvatarSelectionConnection = RunService.RenderStepped:Connect(function()
		self._AvatarHovering = nil
		self._AvatarSelectionHighlight.Adornee = nil

		local Target = Mouse.Target
		if Target then
			local Character = Target:FindFirstAncestorOfClass("Model")
			if Character then
				local Player = Players:GetPlayerFromCharacter(Character)
				if Player then
					self._AvatarHovering = Character
					self._AvatarSelectionHighlight.Adornee = Character
					self.AvatarHovered:Fire(Character)
				end
			end
		end
	end)

	UserInputService.InputEnded:Connect(function(Input, AlreadyProcessed)
		local AcceptableInputTypes = { Enum.UserInputType.MouseButton1, Enum.UserInputType.Touch }
		if (not AlreadyProcessed) and table.find(AcceptableInputTypes, Input.UserInputType) then
			if Mouse.Target then
				local Character = Mouse.Target:FindFirstAncestorOfClass("Model")
				if Character then
					local Player = Players:GetPlayerFromCharacter(Character)
					if (Player ~= nil) and (Player ~= Players.LocalPlayer) then
						self._AvatarSelecting = Character
						self.AvatarSelected:Fire(Character)

						return
					end
				end
			end

			if self._AvatarSelecting then
				self.AvatarDeselected:Fire(self._AvatarSelecting)
			end
			self._AvatarSelecting = nil
		end
	end)
end

function AvatarSelector:Stop()
	if self._AvatarSelectionConnection then
		self._AvatarSelectionConnection:Disconnect()
		self._AvatarSelectionConnection = nil
	end

	self._AvatarSelectionHighlight.Adornee = nil
end

return AvatarSelector
