Hey I was wondering, what is the best way to save data ?
I’ve already a server system but the problem is that I save the data of local player when the player leave but the data of all the players save when the value change often and on a lot of server so I use UpdateAsync()
!
Is a good way to save my data, or there will be a lot of problem ?
How exactly do you save player data? Do you have a single array containing all players or a seperate key for each player? If you use a seperate key for each player SetAsync() makes more sense since only the server the player is actually on will write to the player data key. UpdateAsync() counts towards both your read and write limit while SetAsync() only counts towards the write limit.
There is 2 data that the key is Player.UserID
and one, the same for all of the players, and the key is Players:GetPlayers()
!
So you have a seperate DataStore key for each player? Mind sharing your script to make it easier to understand?
Ok I send you my script it will be easier to understand !
local Players = game:GetService("Players")
local TeleportService = game:GetService("TeleportService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage.Server
local RemoteEvent2 = ReplicatedStorage.GlobalServer
local RemoteEvent3 = ReplicatedStorage.GlobalServer2
local RemoteEvent4 = ReplicatedStorage.JoinedServer
local RemoteEvent5 = ReplicatedStorage.JoinedServer2
local RemoteEvent6 = ReplicatedStorage.GlobalServer3
local DataStoreService = game:GetService("DataStoreService")
local MyServerList = DataStoreService:GetDataStore("MyServerList")
local GlobalServerList = DataStoreService:GetDataStore("GlobalServerList")
local JoinedServerList = DataStoreService:GetDataStore("JoinedServerList")
local SavingValue
local SavingValue2
Players.PlayerAdded:Connect(function(Player)
SavingValue = MyServerList:GetAsync(Player.UserId)
SavingValue2 = JoinedServerList:GetAsync(Player.UserId)
end)
Players.PlayerAdded:Connect(function(Player)
if MyServerList:GetAsync(Player.UserId) ~= nil then
RemoteEvent:FireClient(Player, MyServerList:GetAsync(Player.UserId))
end
end)
Players.PlayerAdded:Connect(function(Player)
if GlobalServerList:GetAsync(Players:GetPlayers()) ~= nil then
RemoteEvent2:FireClient(Player, GlobalServerList:GetAsync(Players:GetPlayers()))
end
end)
Players.PlayerAdded:Connect(function(Player)
if JoinedServerList:GetAsync(Player.UserId) ~= nil then
RemoteEvent4:FireClient(Player, JoinedServerList:GetAsync(Player.UserId))
end
end)
RemoteEvent.OnServerEvent:Connect(function(Player, ServerInfo)
if SavingValue == nil then
SavingValue = {ServerInfo}
else
table.insert(SavingValue, ServerInfo)
end
end)
RemoteEvent.OnServerEvent:Connect(function(Player, ServerInfo)
if GlobalServerList:GetAsync(Players:GetPlayers()) == nil then
GlobalServerList:UpdateAsync(Players:GetPlayers(), function(Data)
return {ServerInfo}
end)
RemoteEvent3:FireAllClients(ServerInfo)
else
local CurrentGlobalServerList = GlobalServerList:GetAsync(Players:GetPlayers())
table.insert(CurrentGlobalServerList, ServerInfo)
GlobalServerList:UpdateAsync(Players:GetPlayers(), function(Data)
return CurrentGlobalServerList
end)
RemoteEvent3:FireAllClients(ServerInfo)
end
end)
RemoteEvent4.OnServerEvent:Connect(function(Player, ServerInfo)
if SavingValue2 == nil then
SavingValue2 = {ServerInfo}
else
table.insert(SavingValue2, ServerInfo)
end
end)
RemoteEvent5.OnServerEvent:Connect(function(Player, ServerInfo)
for Value = 1, #SavingValue2 do
if SavingValue2[Value] ~= nil then
if SavingValue2[Value][1].." / "..SavingValue2[Value][2].." / "..SavingValue2[Value][3] == ServerInfo then
RemoteEvent6:FireClient(Player, ServerInfo)
table.remove(SavingValue2, Value)
end
end
end
end)
Players.PlayerRemoving:Connect(function(Player)
MyServerList:SetAsync(Player.UserId, SavingValue)
JoinedServerList:SetAsync(Player.UserId, SavingValue2)
end)
Make sure you wrap your SetAsync() and GetAsync() calls in a pcall function so you can detect if a Set or Get request fails.
Something along the lines of this:
local success, response = pcall(function()
--Your request method goes here
end)
Yeah, you think it’s worth ?
Have a nice day !