How would I load a players data store to another player?

Hello developers!

I need some help with finding out how to do stuff with data store.
So let’s say that a user wants to look at something another player writed, but that user isn’t in the game anymore.

Something kinda like one of those games where players write notes and they stay in the game forever.

Data Stores should be created or referenced using game:GetService(“DataStoreService”):GetDataStore(“dataStoreName”)

dataStoreName is basically string or number that identifies certain DataStore f.e. dataStoreName can be “UserData”.

:GatDataStore() function itself returns GlobalDataStore instance so it should be called as local or global variable.
(f.e. local userDataStore = game:GetService(“DataStoreService”):GetDataStore(“UserData”)

Saving is done with :SetAsync() function of GlobalDataStore and its syntax is :SetAsync(key: string or number, data: string or number or array) where key is basically a entry in GlobalDataStore. It is safest to use Player.UserId as the key.
Max rate of saving that would not make the script yield is once every 5 seconds.

Loading is done with :GetAsync() funtion of GlobalDataStore and its syntax is :GetAsync(key: string or value) and it returns data saved to certain entry, therefore it has to be called while defining variable.

For safety, all operations related to DataStoreService should be put inside pcall() function, as it may just fail from time to time.

1 Like

Well what im thinking whend the client send the player thats looking for then the serverstorage does a GetAsync(otherplayer.UserId) or something like that

local Players = game.Players

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player,otherplayer)

print(Players:GetUserIdFromNameAsync(otherplayer))
game:GetService("DataStoreService"):GetAsync(Players:GetUserIdFromNameAsync(otherplayer))

end)

These way you can get players ID and there for get the data of that player

1 Like

Well then there’s function :GetPlayerFromUsername() or something somrwhat similar.
It returns Player instance no matter if the player is in game or not.
Then You can load data saved under that player’s UserId

Player:GetUserIdFromNameAsync(Name)
these way you can get the UserId if you have the player full name

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