local t=10 local P=25 local W=game.Players.LocalPlayer local Y=game:GetService("\085\115\101\114\073\110\112\117\116\083\101\114\118\105\099\101")game.ReplicatedStorage.Interactions.Client.Perks.UseXRay.OnClientEvent:connect(function()if W.Character~=nil then local b=W.Character W.PlayerGui.MainGui.Tip.Text="\080\114\101\115\115 \081 \116\111 \117\115\101 \088-\082\097\121"local i=workspace:FindFirstChild("\077\097\112")local D=false local e local h=false e=Y.InputBegan:connect(function(Y)if not D and(not h and(Y.KeyCode==Enum.KeyCode.Q and workspace:FindFirstChild("\077\097\112")==i))then D=true local Y=game.ReplicatedStorage.Interactions.Server.GetPlayingPlayers:InvokeServer()for t,P in pairs(Y)do if P==W then table.remove(Y,t)break end end for P,W in pairs(Y)do local Y=W.Character if Y~=nil and(Y:FindFirstChild("\072\117\109\097\110\111\105\100")and(Y.Humanoid.Health>0 and Y:FindFirstChild("\084\111\114\115\111")))then local P=script.XMarker:clone()P.Parent=Y.Torso spawn(function()wait(t)if P~=nil then P:destroy()P=nil end end)Y.Humanoid.Died:connect(function()if P~=nil then P:destroy()P=nil end end)end end for P=1,t,1 do if h or workspace:FindFirstChild("\077\097\112")==nil then break end W.PlayerGui.MainGui.Tip.Text="\067\097\110 \115\101\101 \111\116\104\101\114\115 \102\111\114 "..((t-P)+1 .." \115\101\099\111\110\100\115")wait(1)end if not h and workspace:FindFirstChild("\077\097\112")then for t=1,P,1 do if h or workspace:FindFirstChild("\077\097\112")==nil then break end W.PlayerGui.MainGui.Tip.Text="\067\097\110 \117\115\101 \097\103\097\105\110 \105\110 "..((P-t)+1 .." \115\101\099\111\110\100\115")wait(1)end if not h and workspace:FindFirstChild("\077\097\112")then W.PlayerGui.MainGui.Tip.Text="\080\114\101\115\115 \081 \116\111 \116\117\114\110 \117\115\101 \088-\082\097\121"end end if h or workspace:FindFirstChild("\077\097\112")==nil then if e~=nil then e:disconnect()e=nil end h=true W.PlayerGui.MainGui.Tip.Text=""end D=false elseif workspace:FindFirstChild("\077\097\112")~=i then if e~=nil then e:disconnect()e=nil end h=true W.PlayerGui.MainGui.Tip.Text=""end end)b.Humanoid.Died:connect(function()if not h then e:disconnect()e=nil h=true W.PlayerGui.MainGui.Tip.Text=""end end)end end)

local XRAY_TIME = 10
local COOLDOWN = 25

local plr = game.Players.LocalPlayer
local inputService = game:GetService("UserInputService")

game.ReplicatedStorage.Interactions.Client.Perks.UseXRay.OnClientEvent:connect(function()
	
	-- Make sure character is alive
	if plr.Character ~= nil then
		local originalChr = plr.Character
		
		-- Show tip
		plr.PlayerGui.MainGui.Tip.Text = "Press Q to use X-Ray"
		
		-- Set map object
		local map = workspace:FindFirstChild("Map")
	
		-- Connect input event
		local debounce = false
		local event
		local done = false
		event = inputService.InputBegan:connect(function(inputObj)
			
			-- Check if states are correct
			if not debounce and not done and inputObj.KeyCode == Enum.KeyCode.Q and workspace:FindFirstChild("Map") == map then
				debounce = true
				
				-- Show location of each player
				local playingPlayers = game.ReplicatedStorage.Interactions.Server.GetPlayingPlayers:InvokeServer()
				for n, i in pairs(playingPlayers) do
					if i == plr then
						table.remove(playingPlayers, n)
						break
					end
				end
				for _, i in pairs(playingPlayers) do
					
					-- Make sure character is alive
					local chr = i.Character
					if chr ~= nil and chr:FindFirstChild("Humanoid") and chr.Humanoid.Health > 0 and chr:FindFirstChild("Torso") then
						
						-- Place X Gui in torso
						local gui = script.XMarker:clone()
						gui.Parent = chr.Torso
						
						-- Remove after 10 seconds
						spawn(function()
							wait(XRAY_TIME)
							if gui ~= nil then
								gui:destroy()
								gui = nil
							end
						end)
						
						-- Connect death
						chr.Humanoid.Died:connect(function()
							if gui ~= nil then
								gui:destroy()
								gui = nil
							end
						end)
						
					end
					
				end
				
				-- Update text to match state
				for i=1, XRAY_TIME do
					if done or workspace:FindFirstChild("Map") == nil then
						break
					end
					plr.PlayerGui.MainGui.Tip.Text = "Can see others for " .. (XRAY_TIME - i + 1) .. " seconds"
					wait(1)
				end
				if not done and workspace:FindFirstChild("Map") then
					for i=1, COOLDOWN do
						if done or workspace:FindFirstChild("Map") == nil then
							break
						end
						plr.PlayerGui.MainGui.Tip.Text = "Can use again in " .. (COOLDOWN - i + 1) .. " seconds"
						wait(1)
					end
					if not done and workspace:FindFirstChild("Map") then
						plr.PlayerGui.MainGui.Tip.Text = "Press Q to turn use X-Ray"
					end
				end
				if done or workspace:FindFirstChild("Map") == nil then
					if event ~= nil then
						event:disconnect()
						event = nil
					end
					done = true
					plr.PlayerGui.MainGui.Tip.Text = ""
				end
				
				debounce = false
			elseif workspace:FindFirstChild("Map") ~= map then
				if event ~= nil then
					event:disconnect()
					event = nil
				end
				done = true
				plr.PlayerGui.MainGui.Tip.Text = ""
			end
			
		end)
		
		-- Connect disconnect
		originalChr.Humanoid.Died:connect(function()
			if not done then
				event:disconnect()
				event = nil
				done = true
				plr.PlayerGui.MainGui.Tip.Text = ""
			end
		end)
	
	end
	
end)
