DataStore Questions

  1. Whats the point of assigning data = myDataStore:GetAsync(UserID)

Will it error if I start doing the save data part?

  1. The reason I asked this question is because before I didn’t use to even do this, but my friend told me it’s better, but I don’t understand why.

local DataStoreService = game:GetService(“DataStoreService”)
local myDataStore = DataStoreService:GetDataStore(“myDataStore”)
local player = game.Players.PlayerAdded

player:Connect(function(plr)
local leaderstats = Instance.new(“Folder”)
leaderstats.Name = “leaderstats”
leaderstats.Parent = plr

local Cash = Instance.new("IntValue")
Cash.Name = "Cash"
Cash.Parent = leaderstats

local UserId = "Player_"..plr.UserId
local Data

local  Succes,ErrorMessage = pcall(function()
	Data = myDataStore:GetAsync(UserId)
end)

end)

It’s getting id of the player and can load correct data to the correct player. It is just assigning data to the player. At this point it is loading the cash for a player.

Loading/Saving data makes an API request to the Roblox website, so yes it has a change to fail and you have to be able to manage it accordingly.

local Success, Error = pcall(function()
	Data = myDataStore:GetAsync(UserId)
end)

if Success then
	print("everything fine data for "..plr.Name.." loaded")
else
	print("data for "..plr.Name.." failed to load you have to handle it")
	--retry loading their data
end

Alright now I understand thanks