Basically i want to know if a player has left the game by leaving from the escape menu or got disconnected is it possible to check that?
Yeah, you could use GuiSevice.MenuOpened to detect if they left. You could make a check for 20 minutes (the number of minutes Roblox leaves you until forcing you to leave).
What’s you goal with this system?
This should work for most unaware players:
--This is a LocalScript
local UserInputService = game:GetService("UserInputService")
local GuiService = game:GetService("GuiService")
local lastInput = os.clock()
local function onUserInput(): ()
lastInput = os.clock()
end
UserInputService.InputBegan:Connect(onUserInput)
UserInputService.TouchStarted:Connect(onUserInput)
--connect more user input events if you like
game.Players.PlayerRemoving:Connect(function(player)
if player ~= game.Players.LocalPlayer then return end
--you can increase the threshold of 19 if you like
local isAfk = (os.clock()-lastInput) > 19
if GuiService.MenuIsOpen and not isAfk then
print("Player left on their own!")
else
--This may not fire if the player is suddenly removed
print("Player Disconnected!")
end
end)
This doesn’t work properly if they use ALT + F4
That’s why I said if they’re unaware, there’re ways to bypass that if you’re smart(for example closing through task manager). For what you mentioned you can add a check for alt and f4 through user input service to check if they press them before the client terminates, if it picks them up.
Short answer:
No way.
Long answer:
There’s no real clean way because there’s a lot of ways to close a client.
I’ll even sort this list out by each method’s ridiculousness:
- pressing leave game on the escape menu
- joining another game while you’re already in one
- being disconnected
- pressing that cute and demure X button on the top right if you’re on windowed mode
- alt-f4ing
- through task manager
- intentionally turning off your network connection
- crashing your client
- crashing your PC
- running out of power
- outright shutting down your PC
- Hyperion/Byfron kicks in
- your cat got into your CPU and ate your components
See what I’m getting at here? That’s a lot of cases you have to account for, some of which you simply have no solution for.
Some of those ways even blur the line between intentionally leaving the game, or being disconnected, and there’s no way you can know what really happened.
A hacky but still unreliable way would be to detect when a player has stood completely still on the server or when their ping is spiking (a telltale sign a player lost or is about to lose connection) - but that can be abused.
Your best bet? Make a feature request.
This is not possible, and also makes no sense, some games punish player from leaving, but you can’t help that, you might add rejoin button or maybe friend join for instance, soo if there are space you can join again
Hey, can you explain what is your final goal with this?
There is currently no viable way of detecting wheter the player closed the tab, left the game by the menu, disconnected, got kicked, crashed, etc.
Does your game punish the player if they leave, and you are trying to make this system to detect if the player disconnected by wifi so they don’t get punished? Even if you achieved that system, which is actually possible but may require some good look into it, it is extremely abusable by just deactivating your wifi connection.