So I’m trying to create upgrades in my game, but I want them to save so first I tought just make a bunch of values in the player, but I did not know if that was the best idea because I had to do like LuckUpgrades and MaxLuckUpgrades for all the upgrades is there another way or a way I can just put a name in a table like this with a module script or something
Name Value
| |
local valueTable = {["LuckUpgrades", 5'],["MaxLuckUpgrades], 10"]}
Then, first, create a folder where all the values go, loop through the table, and create a new IntValue for each value
local valuesFolder = Instance.new("Folder")
valuesFolder.Name = "Values"
valuesFolder.Parent = player
for name, value in pairs(valuesTable) do
local newValue = Instance.new("IntValue")
newValue.Name = name
newValue.Value = value
newValue.Parent = valuesFolder
end
When you are going to save, you can loop through the valuesFolder, change them back to a table, and save them.
function valuesToTable(valuesFolder)
local tbl = {}
for _, intValue in pairs(valuesFolder) do
tbl[intValue.Name] = intValue.Value
end
return tbl
end
function saveData(player)
local valuesFolder = player:FindFirstChild("Values")
if valuesFolder then
local dataToSave = valuesToTable(valuesFolder)
local success, err
local attempt = 1
repeat
success, err = pcall(function()
DataStore:SetAsync(player.UserId, dataToSave)
end)
attempt += 1
until success or attempt > 4
if success then
print(player.Name .. "'s data was success fully saved")
else
print(player.Name .. "'s data failed to save")
end
end
end
Players:PlayerRemoving(saveData)
I got this error
ServerScriptService.leaderstats:25: invalid argument #1 to ‘pairs’ (table expected, got Instance)
at the part I marked
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
local players = game:GetService("Players")
players.PlayerAdded:Connect(function(player)
local valuesTable = {
["LuckUpgrades"] = 5,
["MaxLuckUpgrades"] = 10,
}
local valuesFolder = Instance.new("Folder")
valuesFolder.Name = "Values"
valuesFolder.Parent = player
for name, value in pairs(valuesTable) do
local newValue = Instance.new("IntValue")
newValue.Name = name
newValue.Value = value
newValue.Parent = valuesFolder
end
function valuesToTable(valuesFolder)
local tbl = {}
for _, intValue in pairs(valuesFolder) do ---- HERE IS THE ERROR
tbl[intValue.Name] = intValue.Value
end
return tbl
end
end)
players.PlayerRemoving:Connect(function(player)
local valuesFolder = player:FindFirstChild("Values")
if valuesFolder then
local dataToSave = valuesToTable(valuesFolder)
local success, err
local attempt = 1
repeat
success, err = pcall(function()
DataStoreService:SetAsync(player.UserId, dataToSave)
end)
attempt += 1
until success or attempt > 4
if success then
print(player.Name .. "'s data was success fully saved")
else
print(player.Name .. "'s data failed to save")
end
end
end)
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
local players = game:GetService("Players")
players.PlayerAdded:Connect(function(player)
local valuesTable = {
["LuckUpgrades"] = 5,
["MaxLuckUpgrades"] = 10,
}
local valuesFolder = Instance.new("Folder")
valuesFolder.Name = "Values"
valuesFolder.Parent = player
local data
local success, err = pcall(function()
data = myDataStore:GetAsync(player.UserId)
end)
for name, value in pairs(valuesTable) do
local newValue = Instance.new("IntValue")
newValue.Name = name
newValue.Value = value
newValue.Parent = valuesFolder
if success and data[name] then
newValue.Value = data[name]
end
end
end)
function valuesToTable(valuesFolder)
local tbl = {}
for _, intValue in pairs(valuesFolder:GetChildren()) do ---- HERE IS THE ERROR
tbl[intValue.Name] = intValue.Value
end
return tbl
end
players.PlayerRemoving:Connect(function(player)
local valuesFolder = player:FindFirstChild("Values")
if valuesFolder then
local dataToSave = valuesToTable(valuesFolder)
local success, err
local attempt = 1
repeat
success, err = pcall(function()
myDataStore:SetAsync(player.UserId, dataToSave)
end)
attempt += 1
task.wait(2)
until success or attempt > 4
if success then
print(player.Name .. "'s data was success fully saved")
else
print(player.Name .. "'s data failed to save")
end
end
end)
And make sure to change the values like LuckUpgrade through the server.
You can make changes through the server in test play by going to TEST > Current: Client
One more question I tried adding leaderstats to but it dint work and only the leaderstats save can you please take alook at it but if not thats ok:
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
local players = game:GetService("Players")
players.PlayerAdded:Connect(function(player)
local leaderstatsTable = {
["Steps"] = 0,
["Money"] = 0,
}
local valuesTable = {
["LuckUpgrades"] = 5,
["MaxLuckUpgrades"] = 10,
}
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local valuesFolder = Instance.new("Folder")
valuesFolder.Name = "Values"
valuesFolder.Parent = player
local data
local success, err = pcall(function()
data = myDataStore:GetAsync(player.UserId)
end)
for name, value in pairs(leaderstatsTable) do
local newValue = Instance.new("IntValue")
newValue.Name = name
newValue.Value = value
newValue.Parent = leaderstats
if success and data and data[name] then
newValue.Value = data[name]
end
end
for name, value in pairs(valuesTable) do
local newValue = Instance.new("IntValue")
newValue.Name = name
newValue.Value = value
newValue.Parent = valuesFolder
if success and data and data[name] then
newValue.Value = data[name]
end
end
end)
function leaderstatsToTable(leaderstatsTable)
local tbl = {}
for _, intValue in pairs(leaderstatsTable:GetChildren()) do
tbl[intValue.Name] = intValue.Value
end
return tbl
end
function valuesToTable(valuesFolder)
local tbl = {}
for _, intValue in pairs(valuesFolder:GetChildren()) do
tbl[intValue.Name] = intValue.Value
end
return tbl
end
players.PlayerRemoving:Connect(function(player)
local leaderstatsFolder = player:FindFirstChild("leaderstats")
local valuesFolder = player:FindFirstChild("Values")
if valuesFolder and leaderstatsFolder then
local dataToSave = valuesToTable(valuesFolder) and leaderstatsToTable(leaderstatsFolder)
local success, err
local attempt = 1
repeat
success, err = pcall(function()
myDataStore:SetAsync(player.UserId, dataToSave)
end)
attempt += 1
task.wait(2)
until success or attempt > 4
if success then
print(player.Name .. "'s data was success fully saved")
else
print(player.Name .. "'s data failed to save")
end
end
end)
game:BindToClose(function()
task.wait(2)
end)
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
local myDataStoreLeaderstats = DataStoreService:GetDataStore("myDataStoreLeaderstats")
local players = game:GetService("Players")
players.PlayerAdded:Connect(function(player)
local leaderstatsTable = {
["Steps"] = 0,
["Money"] = 0,
}
local valuesTable = {
["LuckUpgrades"] = 5,
["MaxLuckUpgrades"] = 10,
}
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local valuesFolder = Instance.new("Folder")
valuesFolder.Name = "Values"
valuesFolder.Parent = player
local data
local success, err = pcall(function()
data = myDataStoreLeaderstats:GetAsync(player.UserId)
end)
for name, value in pairs(leaderstatsTable) do
local newValue = Instance.new("IntValue")
newValue.Name = name
newValue.Value = value
newValue.Parent = leaderstats
if success and data and data[name] then
newValue.Value = data[name]
end
end
data
success, err = pcall(function()
data = myDataStore:GetAsync(player.UserId)
end)
for name, value in pairs(valuesTable) do
local newValue = Instance.new("IntValue")
newValue.Name = name
newValue.Value = value
newValue.Parent = valuesFolder
if success and data and data[name] then
newValue.Value = data[name]
end
end
end)
function valuesToTable(valuesFolder)
local tbl = {}
for _, intValue in pairs(valuesFolder:GetChildren()) do
tbl[intValue.Name] = intValue.Value
end
return tbl
end
players.PlayerRemoving:Connect(function(player)
local leaderstatsFolder = player:FindFirstChild("leaderstats")
local valuesFolder = player:FindFirstChild("Values")
if valuesFolder and leaderstatsFolder then
local dataToSave = valuesToTable(valuesFolder)
local dataToSaveLeaderstats = valuesToTable(leaderstatsFolder)
local success, err
local attempt = 1
repeat
success, err = pcall(function()
myDataStore:SetAsync(player.UserId, dataToSave)
myDataStoreLeaderstats:SetAsync(player.UserId, dataToSaveLeaderstats)
end)
attempt += 1
task.wait(2)
until success or attempt > 4
if success then
print(player.Name .. "'s data was success fully saved")
else
print(player.Name .. "'s data failed to save")
end
end
end)
game:BindToClose(function()
task.wait(2)
end)