I have multiple currencies in this game (6 to be exact). I also have StringValues that link to swords that need to be saved if the player has said sword. I would like to know the most efficient way of saving and loading number values and string values at the same time and also why this doesn’t work
for cycle, object in ipairs(leaderstats:GetDescendants()) do
if object:IsA("NumberValue") then
data = GameData:GetAsync(Pid.."-Stats"..object.Name)
object.Value = (data)
end
end
You’re updating stats in a datastore too fast, I would suggest saving all the data in one stat and saving it once.
I would suggest putting it into one datastore and using tables
example
local StatStore = game:GetService('DataStoreService'):GetDataStore('StatStore')
-- put this somewhere where you save the stats
local data = {}
for i,v in pairs(leaderstats:GetChildren()) do -- loops through the stats
data[v.Name] = v.Value -- puts all the stats into one table
end
StatStore:SetAsync('Player_'..player.UserId, data) -- saves the table in one store
-- put this where you load data
local data = StatStore:GetAsync('Player_'..player.UserId)
if data then
for i,v in pairs(leaderstats:GetChildren()) do
local value = data[v.Name]
if value then
v.Value = value
end
end
end
I just tried this, It doesn’t throw any errors but it doesn’t load the stats.
Is there anything wrong with this?
players.PlayerAdded:Connect(function(player)
local Int = Instance.new("IntValue",player) --[[//--]] Int.Name = ("leaderstats")
local kos = Instance.new("NumberValue",Int)--[[//--]] kos.Name = ("KOs")
local points = Instance.new("NumberValue",Int)--[[//--]]points.Name = "Points"
local vc = Instance.new("NumberValue",Int)--[[//--]]vc.Name = ("VirtualCurrency")
local lvl = Instance.new("NumberValue",Int)--[[//--]]lvl.Name = ("lvl")
local xp = Instance.new("NumberValue",lvl)--[[//--]]xp.Name = ("XP")
local thorium = Instance.new("NumberValue",Int)--[[//--]]thorium.Name = ("Thorium")
local Swords = Instance.new("Folder",player)--[[//--]]Swords.Name = "Swords"
wait(1)
local data = GameData:GetAsync('Player_'..player.UserId)
if data then print("Loaded data")
for i,v in pairs(Int:GetDescendants()) do
local value = data[v.Name]
if value then
v.Value = value
end
end
else
print("no data")
end
end)
players.PlayerRemoving:Connect(function(player)
local data = {}
for i,v in pairs(player.leaderstats:GetDescendants()) do -- loops through the stats
data[v.Name] = v.Value -- puts all the stats into one table
end
GameData:GetAsync("Player_"..player.UserId, data)
print("SAved")
end)