Checking if player is in game

Ayo, Was wondering if there is a better way doing this code:

Server:

local event = game:GetService("ReplicatedStorage").CheckPlayer

local text = game.StarterGui.MainUI.Frame.TextBox


event.OnServerEvent:Connect(function(p, ingame)
	if ingame == "PlayerInGame" then
		.
	elseif ingame == "PlayerNotInGame" then
		.
	end
end)

Local:

local event = game:GetService("ReplicatedStorage").CheckPlayer

script.Parent.MouseButton1Click:Connect(function()
	if game.Players:FindFirstChild(script.Parent.Parent.TextBox.Text) then
		event:FireServer("PlayerInGame")
	elseif not game.Players:FindFirstChild(script.Parent.Parent.TextBox.Text) then
		event:FireServer("PlayerNotInGame")
	end
end)

In this case, I think it’s a bit redundant. A player can’t fire a remote if they’ve disconnected from the game. If they’ve fired a RemoteEvent, you should be safe to assume they are in-game. As a side-note, I would handle your sanity checks on the server versus checking on the client and having the client then send the server the value, since it’s less secure that way.

1 Like

If you got the player name you can just do the following on the client/server (your choice)

local playername = "blallba"
local suc, _ = pcall(function()
     local a = game.Players[playername]
end)
if suc then
-- do things
end

probally not that efficient but its a way

Why so complicated system? Hackers can’t delete players so you can just do in client if game.Players:FindFirstChild()

1 Like

This may help you.

You don’t necessarily need to make a table for storing players and checking if they left or not, but I wouldn’t recommend using Remote Events since exploiters can get to that.