How do I store a 'place data'?

  1. What do you want to achieve? I want to store data (In array) how much players ever visited a game?

  2. What is the issue? So currently I’m trying to store a data in array how much players ever joined the game. So when you run the game it have to print a table with list of player who ever visited the game.

1 Like

Problem 1. Overflow, there is a limit defined in the space time and roblox. To store that information (that is a lot of information btw) into 1 table you best oof your datastore.
Problem 2. How do you even save data? Just save the Player.UserId and insert it into the table?
Basic example:

game.Players.PlayerAdded:Connect(function(player)
    local key = "id_"..player
    local data 
    local success, err = pcall(function()
        data = ds:GetAsync(key)
    end)
    
    if success then
       if not table.find(playervisitsdata, data) then
          table.insert(playervisitdata, data) 
       else
          print(data)
       end
    else
       warn(err)
    end
end)
1 Like

thank you very much, but what is variables:

  • playervisitsdata
  • playervisitdata
1 Like

Are you trying to get how many total players have visited the game or how many times a single player has joined the game?

I’m trying to register every unique player who ever joined the game. So when player joined at the place for the first time he will get registered to the table.

1 Like

So you just want a system that detects if a player has joined before or not?

If that is the case, there are already several attempts at this:

Read through these and see if this is what you are looking for.

If not, let me know and I can help you further, but I am still having issues determining exactly what you want.

it’s closer but not yet. I’m trying to register all UNIQUE players to the table, let me show you example:
Table is empty
*player1 joins to the game
Table is now {player1}
Firing all other server to update current table value
*player1 leaves
Tabe is still contains him
*player2 joins
Table is now {player1, player2}

I think I got it now.

It is probably best to use MessageService in this case.

I haven’t really used it much, however, there are several resources out there for it.

The best way to do this is probably to convert the player’s UserId to a string and then send it to every other server. Then, when the other servers receive they can convert it back to a number and insert them into the table.

The only problems I see is saving the table and potential failure in sending it across servers.

(Note: I would recommend storing the player’s UserId in the table instead of their player instance, as you can’t save instances and can always just get the player by their UserId from the Players service.)

I hope this helps get you started and let me know if you have any other questions or something was unclear.

Yes, I heard about messaging service. But mainly problem is how to store this data? So when new player run the game he can see list of unique player Ids who ever played experience. (I mean log in output)

Check out this post. It might give you an idea of how you could make it work:

In general, you need to create a DataStore for the player to track whether they are entering the game for the first time. After that, you can simply create an additional DataStore for other purposes

Also, don’t forget to save the values after the player exits the game

local DSS = game:GetService("DataStoreService")
local DataStorePlayer = DSS:GetDataStore("Players")
local DataStoreNewPlayer = DSS:GetDataStore("NewPlayers")

game.Players.PlayerAdded:Connect(function(Player)
	
	local VisitGame = Instance.new("BoolValue",Player)
	VisitGame.Name = "VisitGame"
	VisitGame.Value = false
	local PlrKey = tostring(Player.UserId)
	local LoadData = DataStorePlayer:GetAsync(PlrKey)
	if LoadData then
		VisitGame.Value = LoadData["VisitGame"]
	end
	if VisitGame.Value == false then
		VisitGame.Value = true
		DataStoreNewPlayer:UpdateAsync("PlayersNew",function(Data)
			if Data ~= nil then
				local UpdateData = Data
				UpdateData["Players"] = UpdateData["Players"] + 1
				return UpdateData
			elseif Data == nil then
				local NewData = {
					["Players"] = 1,
				}
				return NewData
			end
		end)
	end
end)
2 Likes

Thank you very much! It is solution, by the way I a bit improved this code:

local Players = game:GetService('Players')
local DataStoreService = game:GetService("DataStoreService")
local DataStorePlayer = DataStoreService:GetDataStore("Player")
local DataStoreNewPlayer = DataStoreService:GetDataStore("NewPlayer")

Players.PlayerAdded:Connect(function(Player: Player)
	Player:SetAttribute('VisitGame', false)
	local PlrKey = tostring(Player.UserId)
	local LoadData = DataStorePlayer:GetAsync(PlrKey)
	if LoadData then
		Player:SetAttribute('VisitGame', LoadData["VisitGame"])
	end
	if not Player:GetAttribute('VisitGame') then
		Player:SetAttribute('VisitGame', false)
		DataStoreNewPlayer:UpdateAsync("PlayersNew",function(Data)
			if Data  then
				local UpdateData = Data
				if not table.find(UpdateData, Player.UserId) then
					UpdateData[#UpdateData + 1]  = Player.UserId
				end 
				return UpdateData
			else
				return {Player.UserId}
			end
		end)
	end
end)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.