:SetAsync() Question

Hello! I’m trying to make a DataStore for my game that is as efficient to load and save players data.

I have conflicting views about reading the sources of other topics about using SetAsync or using UpdateAsync so I decided to make a separate post about it.

If I have a rarely changing value that can change from anywhere from 1-2000 how should I save the value?

If i have a frequently changing value (like coins) how should I save the value?

(I have tried DataStore2 and it did not fulfill what I needed it for)

If your saving a players stuff such as coins or gear, you should use setAsync when they leave the game. If you are saving a value that all the server use at once, you should use UpdateAsync.

1 Like

What does DataStore2 not fulfill? Because it can do anything that the normal datastores can do, unless you’re saving data that isn’t about a player, which would be unnecessary

I need to edit player’s data that is not in the game, and I have already created a new system to save data that is efficient for my use, and lets me use it to the full extent.

¿You mean you want to save the value constantly and not on the PlayerRemoving?

I need to save players data periodically (lets say every two minutes) in game as well as being able to save players’ data who are NOT in the game; editing players’ data out of server

I dont think you can save data for a player who has not joined in to the game.

I found this and then realized I needed a way to do this with my new admin system if i wanted to be able to ban players not in the game

I’m not familiar with datastore2, but if it uses userId to save data to players then you can ban/unban using GetUserIdFromNameAsync(name) (if its a command then the user of the command must type the full name.)

You can save both values with SetAsync. If you’re worried about different moderators adding different users at the same time (to the ban list), you can take a look into UpdateAsync. UpdateAsync is very similar to SetAsync. It’s basically a queued version (so it waits for the previous data to be saved before starting the next save).


@TheDestroyer0525 Please don’t spread misinformation. You can edit a player’s data as long as you know their data key.

1 Like

Well, I was not sure at all, anyways thanks for letting me know it will help me also in my projects, greetings!

i do something like this

local playersService = game:GetService("Players")
local dataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("Player")
local playerData = {}

-- prevent the server from closing until all player data has been saved
game:BindToClose(function()
	while(next(playerData)) do
		task.wait()
	end
end)

-- when a player enters the game load there data from the datastore
playersService.PlayerAdded:Connect(function(player)
	local success, value = pcall(dataStore.GetAsync, dataStore, player.UserId)
	if success == false then return end
	playerData[player] = value or {}

	-- if you want to send all data to the player you can do it like this
	remoteEvent1:FireClient(player, playerData[player])
end)
 
-- when a player leaves save there data to the datastore
playersService.PlayerRemoving:Connect(function(player)
	if playerData[player] == nil then return end
	local success, value = pcall(dataStore.SetAsync, dataStore, player.UserId, playerData[player])
	playerData[player] = nil
end)

-- BindableFunction that other scripts can use to get a players data
script.Get.OnInvoke = function (player, key)
	if playerData[player] == nil then return end
	return playerData[player][key]
end

-- BindableFunction that other scripts can use to save player data
script.Set.OnInvoke = function (player, key, value)
	if playerData[player] == nil then return end
	playerData[player][key] = value

	-- if you want to send this data to the player you can do it like this
	remoteEvent2:FireClient(player, key, value)
end

if you don’t want to use BindableFunctions its also possible to do the same with a module

1 Like

UpdateAsync calls GetAsync and SetAsync (hence why it gives you the current data). If you need session-locking UpdateAsync is useful.