Im currently making a game that has about 20+ stats per player and I have noticed that if a lot of people are in a server the datastore requests become full so I found that you can save a table for the stats and then 1 datastore for the table. I have tried to do this but I can not figure out how to save the table. I would like to do it where I dont have to type a new value for each stat if possible but if not I can do it.
Hi you can do this by creating a dictionary and setting the values you want
local t = {}
game.Players.PlayerAdded:Connect(function(Player)
t[Player.UserId] = {} -- create another table for each individual player
t['Money'] = 50 -- create a value for Money or whatever you are trying to save
--create more values if you have more
--Saving the table
Datastore:SetAsync(Player.UserId, t[Player.UserId]) -- saves the table using the players userid as the key
end)
You forgot to put access the player’s unique table in there to save it. You set t.Money and not t[UserId].Money
local t = {}
game.Players.PlayerAdded:Connect(function(Player)
t[Player.UserId] = {} -- create another table for each individual player
t[Player.UserId]['Money'] = 50 -- create a value for Money or whatever you are trying to save
--create more values if you have more
--Saving the table
Datastore:SetAsync(Player.UserId, t[Player.UserId]) -- saves the table using the players userid as the key
end)
1 Like
You can just add all the key’s or values to the table and save that table. SetAsync(key, table)
local t = {}
t.test = "b" -- sugar for t["test"]
DataStore:SetAsync(key, t)
Here’s something you can use.
local ds = game:GetService("DataStoreService"):GetDataStore("PlayerData")
local PlayerStats = {
["Cash"] = 0,
["Gems"] = 0,
["Power"] = 0,
}
game.Players.PlayerAdded:Connect(function(plr)
local Prefix = plr.UserId
local Stats = Instance.new("Folder", plr)
Stats.Name = "Stats"
for i,v in pairs(PlayerStats) do
local NewInst = Instance.new("IntValue", Stats)
NewInst.Name = i
NewInst.Value = v
end
local Data = nil
pcall(function()
Data = ds:GetAsync(Prefix)
end)
if Data ~= nil then
for i,v in pairs(Stats:GetChildren()) do
v.Value = Data[v.Name]
end
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local Prefix = plr.UserId
local Stats = plr.Stats
pcall(function()
if Stats then
local SaveData = {}
for i,v in pairs(Stats:GetChildren()) do
SaveData[v.Name] = v.Value
end
ds:SetAsync(Prefix, SaveData)
end
end)
end)
[/quote]
1 Like