DataStore Saving Players

Hi! Im trying to save players to a datastore. How can I add players to a datastore, and see if a player is saved in the datastore?

1 Like

Save a userid. UserId’s never change. Then when your searching for a player, you can instead search for a Player’s Player.UserId instead of their name.

1 Like

How can I save multiple players? And then get the multiple players in the datastore?

1 Like

Maybe assign a unique group id for that set of players, that group id is the key you will be getting. Then the saved data is a table of all the players PlayerId.

1 Like

I don’t understand what you mean.

1 Like
local tabletosave = {123456,654321,024680}

local groupid = --whatever code/function you need to execute/call in order to generate a group id

From here, the Datastore key will be the groupid variable. You save the tabletosave table inside of that datastore key.

Can I use the table and make a key like this:

:SetAsync(tabletosave, “Beast”)

Or does it need to be a new key every time its set?

1 Like

If you follow my example, this is the SetAsync line of code you would use.

OrderedDatastore:SetAsync(tostring(groupid), tabletosave)

When getting the contents of the datastore, you still have the contents of tabletosave, so you can use a for loop to unload those.

local rotation = 0 -- this will be used if you want to find out how many PlayerId's were identified
for i, PlayerId in pairs(RetrievedTableVariable) do
local UserId[i] = tonumber(PlayerId)
rotation = i
end

Now I’ve never used a for loop in that way, but I’m pretty sure you can assign a variable like that. Then you can just rewrite the variable to your choosing using the UserId variable set by the for loop.

local PlayerId1 = UserId1

Hope this helps.

How do I get the table? Using :GetAsync(groupid)?

Yup! In the example above, that would be identified as RetrievedTableVariable.

1 Like

So I do

local RetrievedTableVariable = DataStore:GetAsync(groupid)

Yes, this will return a variant, which you can now use in a for loop to assign to a new variable for use in another function. You can send it through a BindableEvent if it is required in another ServerScript, or a RemoteEvent if it is required in a local script.

When using events, they accept tuple arguments, so you have a couple of options.

A. Send the individual player id’s as different arguments.
B. Send the RetrievedTableVariable variable through as the single tuple argument, so that the script can do the for loop and individually assign those.

Remember that when using a remote event, the first argument you should pass through is the player, unless you are using the :FireAllClients function.

1 Like