How should I go about saving player usernames in a DataStore?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I’m trying to script a DataStore that will store the username of anyone who has ever joined my game.

  1. What is the issue? Include screenshots / videos if possible!

When I use DataStore:GetAsync() and DataStore:SetAsync(), there is an overload of DataStore requests for the same key. The reason it’s happening is because it’s a global DataStore that uses the same key across all servers, not unique to a specific player UserId. Since there’s several hundreds of servers all doing DataStore:GetAsync() and DataStore:SetAsync() for the exact same key, it creates several hundreds of requests, causing the overload.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I tried finding a solution online, but I was unsuccessful.

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")

local UsernameStore = DataStoreService:GetDataStore("Usernames_T2")

local Usernames = {}

Players.PlayerAdded:Connect(function(Player)
	
	local PlayerUsername = Player.Name
	local UserId = Player.UserId
	
	local Success, Error = pcall(function()
		Usernames = UsernameStore:GetAsync("Usernames")
	end)
	if Usernames == nil then Usernames = {} end
	if not Success then
		warn(Error)
	end
	
	if not table.find(Usernames, PlayerUsername) then
		table.insert(Usernames, PlayerUsername)
		local Success2, Error2 = pcall(function()
			UsernameStore:SetAsync("Usernames", Usernames)
		end)
		if not Success2 then 
			warn(Error2)
		end
	end
	
end)

This is not how you use Datastores. A single Datastore instance itself acts like a table. I have no idea what you’re trying to do here:

Uh huh here’s an entire article from the documentation that meticulously breaks down how you should be using Datastore for storing player data
https://create.roblox.com/docs/cloud-services/datastores

It may make sense if you are using a DataStore Plugin that looks for all saved instances of stored usernames.

Not if every single server is trying to overwrite each other with what’s actually being saved inside Datastore. Nothing actually gets saved that way because there is no mutual respect between servers.

Some DataStore plugins allow for one to see all DataStores saved under a specific parameter-----they may be better off having a DataStore that detects whether or not a player has played.

That’s not the point. What the OP is currently doing is not a good idea (for reason listed above) and I am encouraging them to follow Roblox’s own methodology of using Datastores. It has nothing to do with plugins.

Sorry, but linking the documentation isn’t really helpful to me. I know how to use DataStores for saving player data like normal, but I need a way to have a DataStore that will store the usernames of all players who have ever joined my game. This is a different case from simply saving player data.

I’m trying to make a global search system and I planned on filtering through a table with all the usernames for it. That’s the reason I’m not using a key for each player individually: I need a way to access all player usernames in one table.

Use the datastore as a hash table {UserId = true}. Then just use :ListKeysAsync() to get all players, similar to how you would get a leaderboard.

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