-- General settings

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)
