Is it possible to check if a player is online, and in your game?

I've got a system, but it's not working the way I wanted it too.

local function savePlayerData(player)
	if sessionData[player] then
		return dataStoreRetry(function()
			return FriendData:SetAsync(player.userId, sessionData[player])
		end)
	end
end

local function setupPlayerData(player)
	local success, data = getPlayerData(player)
	if not success then
		sessionData[player] = false
	else
		if not data then
			sessionData[player] = {}
			savePlayerData(player)
		else
			sessionData[player] = data
		end
	end
end

function DataStore:UpdateStatus(player, isOnline)
	for i = 1, #sessionData[player] do
		local friendID = game.Players:GetUserIdFromNameAsync(sessionData[player][i]["Name"])
		if friendID ~= nil then
			output(player, ":", friendID)
		end
		
		local playerData = FriendData:GetAsync(friendID)
		
		if playerData ~= nil then
			for i2 = 1, #playerData do
				for i3 = 1, #playerData[i2] do
					if playerData[i2][i3]["Name"] == player.Name then
						playerData[i2][i3]["Online"] = isOnline
					end
				end
			end
			
			FriendData:SetAsync(game.Players:GetUserIdFromNameAsync(sessionData[player][i]["Name"], playerData))
		end
	end
end

game.Players.PlayerAdded:connect(function(plr)
	setupPlayerData(plr)
	DataStore:UpdateStatus(plr, true)
end)

game.Players.PlayerRemoving:connect(function(plr)
	DataStore:UpdateStatus(plr, false)
	savePlayerData(plr)
end)

5 comments

0

"Is it possible to check if a player is online, and in your game?" Yes. If they're in your game, they must be online. Therefore: if game.Players:FindFirstChild("bob") then

1

Yeah, not sure what you're asking exactly. There's no way of checking if they're online if they're not in you game I don't believe. =

0

I think he's wanting to see if he can get a player from another server, which I assume is possible.

0

@PreciseLogic: This is what I was after.. I want to be able to get if they're in my game, but on a different server. Hints the whole friend things.

0