Oh, I mistook the “Global Table” for some thing I diecovered recently that uses a _G. I have changed that, but where should I move the transferring so it doesn’t loop through everyone?
NOTE: I spent a few days attempting to revamp the script. Here are some changes:
-
The game creates a folder and then loads the data from the table. It then transfers it within the specified location
-
The output now displays the message “Data has been successfully saved/loaded”. (Still doesn’t work)
*The game saves the main folder, therefore saving all of its children. The loading occurs AFTER the default stats have been applied so that they don’t override saved data.
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
local plrStats
local weapons
local materials
local summoning
local currency
game.Players.PlayerAdded:Connect(function(player)
plrStats = {
["Element"] = 0, --1(Fire),2(Water),3(Ice),4(Nature),5(Lightning),6(Earth),7(Light),8(Dark),9(Psychic)
--Customizable Stats
["Skin Tone"] = 1,
["Hair"] = 0,
["Hat"] = 0,
["Shirt"] = 1,
["Pants"] = 1,
["Shoes"] = 1,
--Base Stats as Integers
["Level"] = 1,
["Base HP"] = 100,
["Base Atk"] = 10,
["Base Def"] = 0,
["Base MP"] = 10,
["Base Crit %"] = .05,
["Base Crit Dmg"] = .5,
["Base Shield DMG Resist"] = 0.00,
--Added Stats
["HP Bonus"] = 0,
["Atk Bonus"] = 0,
["Def Bonus"] = 0,
["MP Bonus"] = 0,
["Crit% Bonus"] = 0.00,
["Crit Dmg Bonus"] = 0.00,
["Shield DMG Resist Bonus"] = 0,
--Equipped Items
["Current Sword"] = 1,
["Current Shield"] = 1
}
local playerFolder = Instance.new("Folder")
playerFolder.Name = "PlayerFolder"
playerFolder.Parent = player
for i,v in pairs(plrStats) do
print("'"..i.."'","= '"..v.."'")
local value = Instance.new("IntValue")
value.Name = i
value.Value = v
value.Parent = playerFolder
end
-----------------------------------------------------------------
weapons = {
}
local weaponFolder = Instance.new("Folder")
weaponFolder.Name = "Weapons"
weaponFolder.Parent = playerFolder
for i,v in pairs(weapons) do
print("'"..i.."'","= '"..v.."'")
local value = Instance.new("IntValue")
value.Name = i
value.Value = v
value.Parent = weaponFolder
end
-----------------------------------------------------------------
summoning = {
["Summon Tokens"] = 5,
["Legendary Pity"] = 0,
["Epic Pity"] = 0,
["Heavenly Pity"] = 0
}
local summoningFolder = Instance.new("Folder")
summoningFolder.Name = "Summoning"
summoningFolder.Parent = playerFolder
for i,v in pairs(summoning) do
print("'"..i.."'","= '"..v.."'")
local value = Instance.new("IntValue")
value.Name = i
value.Value = v
value.Parent = summoningFolder
end
-----------------------------------------------------------------
materials = {
}
local materialFolder = Instance.new("Folder")
materialFolder.Name = "Materials"
materialFolder.Parent = playerFolder
for i,v in pairs(materials) do
print("'"..i.."'","= '"..v.."'")
local value = Instance.new("IntValue")
value.Name = i
value.Value = v
value.Parent = materialFolder
end
-----------------------------------------------------------------
currency = {
["Credits"] = 10000
}
local currencyFolder = Instance.new("Folder")
currencyFolder.Name = "Currencies"
currencyFolder.Parent = playerFolder
for i,v in pairs(currency) do
print("'"..i.."'","= '"..v.."'")
local value = Instance.new("IntValue")
value.Name = i
value.Value = v
value.Parent = currencyFolder
end
-----------------------------------------------------------------
local data
local success, errormessage = pcall(function()
data = myDataStore:GetAsync(player.UserId.."-DATA")
end)
if success then
print("Data Loaded Successfully")
else
warn("Error loading Save Data",errormessage)
end
end)
function Save(player)
local success, errormessage = pcall(function()
myDataStore:SetAsync(player.UserId.."-DATA", plrStats)
end)
if success then
print("Data Successfully Saved.")
else
warn("There was an Error Saving Data",errormessage)
end
end
game.Players.PlayerRemoving:Connect(function(player)
Save(player)
end)
game:BindToClose(function()
for i, player in pairs(game.Players:GetPlayers()) do
Save(player)
end
end)
Note that that while the output has said that the game saves, it doesn’t. There are no errors in the output.
The problem is that with this line you are settings the value of plrStats.inventoty.weapons to a table containg a folder which is an instance and instances cannot be saved on datastores, so you will need to loop through all the items in the folder and add them to the table using table.insert()
Again, you are saving an instance and not its value, add .Value
afte``player.leaderstats.Level.` to save the value and not the instance
Only use SetAsync()
when the player has no data, if data for the player was already saved you should use UpdateAsync()
to prevent data loss
This is what I have now:
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
local plrStats
local weapons
local materials
local summoning
local currency
local customs
local baseStats
local addStats
game.Players.PlayerAdded:Connect(function(player)
plrStats = {
}
local playerFolder = Instance.new("Folder")
playerFolder.Name = "PlayerFolder"
playerFolder.Parent = player
for i,v in pairs(plrStats) do
print("'"..i.."'","= '"..v.."'")
local value = Instance.new("IntValue")
value.Name = i
value.Value = v
value.Parent = playerFolder
table.insert(plrStats, v)
end
-----------------------------------------------------------------
weapons = {
}
local weaponFolder = Instance.new("Folder")
weaponFolder.Name = "Weapons"
weaponFolder.Parent = playerFolder
for i,v in pairs(weapons) do
print("'"..i.."'","= '"..v.."'")
local value = Instance.new("IntValue")
value.Name = i
value.Value = v
value.Parent = weaponFolder
table.insert(plrStats, v)
end
-----------------------------------------------------------------
summoning = {
["Summon Tokens"] = 5,
["Legendary Pity"] = 0,
["Epic Pity"] = 0,
["Heavenly Pity"] = 0
}
local summoningFolder = Instance.new("Folder")
summoningFolder.Name = "Summoning"
summoningFolder.Parent = playerFolder
for i,v in pairs(summoning) do
print("'"..i.."'","= '"..v.."'")
local value = Instance.new("IntValue")
value.Name = i
value.Value = v
value.Parent = summoningFolder
table.insert(plrStats, v)
end
-----------------------------------------------------------------
materials = {
}
local materialFolder = Instance.new("Folder")
materialFolder.Name = "Materials"
materialFolder.Parent = playerFolder
for i,v in pairs(materials) do
print("'"..i.."'","= '"..v.."'")
local value = Instance.new("IntValue")
value.Name = i
value.Value = v
value.Parent = materialFolder
table.insert(plrStats, v)
end
-----------------------------------------------------------------
currency = {
["Credits"] = 10000
}
local currencyFolder = Instance.new("Folder")
currencyFolder.Name = "Currencies"
currencyFolder.Parent = playerFolder
for i,v in pairs(currency) do
print("'"..i.."'","= '"..v.."'")
local value = Instance.new("IntValue")
value.Name = i
value.Value = v
value.Parent = currencyFolder
table.insert(plrStats, v)
end
-----------------------------------------------------------------
customs = {
["Element"] = 0, --1(Fire),2(Water),3(Ice),4(Nature),5(Lightning),6(Earth),7(Light),8(Dark),9(Psychic)
["Skin Tone"] = 1,
["Hair"] = 0,
["Hat"] = 0,
["Shirt"] = 1,
["Pants"] = 1,
["Shoes"] = 1
}
local customsFolder = Instance.new("Folder")
customsFolder.Name = "Customs"
customsFolder.Parent = playerFolder
for i,v in pairs(customs) do
print("'"..i.."'","= '"..v.."'")
local value = Instance.new("IntValue")
value.Name = i
value.Value = v
value.Parent = customsFolder
table.insert(plrStats, v)
end
-----------------------------------------------------------------
baseStats = {
["Level"] = 1,
["Base HP"] = 100,
["Base Atk"] = 10,
["Base Def"] = 0,
["Base MP"] = 10,
["Base Crit %"] = .05,
["Base Crit Dmg"] = .5,
["Base Shield DMG Resist"] = 0.00
}
local baseFolder = Instance.new("Folder")
baseFolder.Name = "Base Stats"
baseFolder.Parent = playerFolder
for i,v in pairs(baseStats) do
print("'"..i.."'","= '"..v.."'")
local value = Instance.new("IntValue")
value.Name = i
value.Value = v
value.Parent = baseFolder
table.insert(plrStats, v)
end
-----------------------------------------------------------------
addStats = {
["HP Bonus"] = 0,
["Atk Bonus"] = 0,
["Def Bonus"] = 0,
["MP Bonus"] = 0,
["Crit% Bonus"] = 0.00,
["Crit Dmg Bonus"] = 0.00,
["Shield DMG Resist Bonus"] = 0,
}
local addFolder = Instance.new("Folder")
addFolder.Name = "Additional Stats"
addFolder.Parent = playerFolder
for i,v in pairs(addStats) do
print("'"..i.."'","= '"..v.."'")
local value = Instance.new("IntValue")
value.Name = i
value.Value = v
value.Parent = addFolder
table.insert(plrStats, v)
end
print(plrStats)
-----------------------------------------------------------------
local data
local success, errormessage = pcall(function()
data = myDataStore:GetAsync(player.UserId.."-DATA", player.PlayerFolder)
end)
if success then
print("Data Loaded Successfully")
else
warn("Error loading Save Data",errormessage)
end
end)
function Save(player)
local success, errormessage = pcall(function()
myDataStore:UpdateAsync(player.UserId.."-DATA", plrStats)
end)
if success then
print("Data Successfully Saved.")
else
warn("There was an Error Saving Data",errormessage)
end
end
game.Players.PlayerRemoving:Connect(function(player)
Save(player)
end)
game:BindToClose(function()
for i, player in pairs(game.Players:GetPlayers()) do
Save(player)
end
end)
There IS an error in the output now, and I’m not sure how to load the table date back into the folder. The error message says " There was an Error Saving Data Unable to cast value to function" – Line 205 (The warn in the Save function)
To load the values would I have to loop through each table’s saved data and then override the default value for that data item?
Also I just noticed the :GetAsync() was getting the wrong thing. I will retry the testing.
Edit: Still doesn’t work when changing the Get Async to the plrStats Table.
Whenever I put it as UpdateAsync(), a warning appears in the output. When I use SetAsync(), the game prints that it successfully saved.
Here is what I have now:
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
local plrStats
local weapons
local materials
local summoning
local currency
local customs
local baseStats
local addStats
game.Players.PlayerAdded:Connect(function(player)
plrStats = {
weapons = {},
materials = {},
summoning = {
["Summon Tokens"] = 10,
["Legendary Pity"] = 0,
["Epic Pity"] = 0,
["Heavenly Pity"] = 0},
currency = {
["Credits"] = 10000},
customs = {
["Element"] = 0, --1(Fire),2(Water),3(Ice),4(Nature),5(Lightning),6(Earth),7(Light),8(Dark),9(Psychic)
["Skin Tone"] = 1,
["Hair"] = 0,
["Hat"] = 0,
["Shirt"] = 1,
["Pants"] = 1,
["Shoes"] = 1},
baseStats = {
["Level"] = 1,
["Base HP"] = 100,
["Base Atk"] = 10,
["Base Def"] = 0,
["Base MP"] = 10,
["Base Crit %"] = .05,
["Base Crit Dmg"] = .5,
["Base Shield DMG Resist"] = 0.00},
addStats = {
["HP Bonus"] = 0,
["Atk Bonus"] = 0,
["Def Bonus"] = 0,
["MP Bonus"] = 0,
["Crit% Bonus"] = 0.00,
["Crit Dmg Bonus"] = 0.00,
["Shield DMG Resist Bonus"] = 0}
}
local data
local success, errormessage = pcall(function()
data = myDataStore:GetAsync(player.UserId.."-DATA", plrStats)
end)
if success then
print("Data Loaded Successfully")
else
warn("Error loading Save Data",errormessage)
end
local playerFolder = Instance.new("Folder")
playerFolder.Name = "PlayerFolder"
playerFolder.Parent = player
-----------------------------------------------------------------
local weaponsTable = plrStats.weapons
local weaponFolder = Instance.new("Folder")
weaponFolder.Name = "Weapons"
weaponFolder.Parent = playerFolder
for i,v in pairs(weaponsTable) do
print("'"..i.."'","= '"..v.."'")
local value = Instance.new("IntValue")
value.Name = i
value.Value = v
value.Parent = weaponFolder
table.insert(plrStats, v)
end
-----------------------------------------------------------------
local summonTable = plrStats.summoning
local summoningFolder = Instance.new("Folder")
summoningFolder.Name = "Summoning"
summoningFolder.Parent = playerFolder
for i,v in pairs(summonTable) do
print("'"..i.."'","= '"..v.."'")
local value = Instance.new("IntValue")
value.Name = i
value.Value = v
value.Parent = summoningFolder
table.insert(plrStats, v)
end
-----------------------------------------------------------------
local materialTable = plrStats.materials
local materialFolder = Instance.new("Folder")
materialFolder.Name = "Materials"
materialFolder.Parent = playerFolder
for i,v in pairs(materialTable) do
print("'"..i.."'","= '"..v.."'")
local value = Instance.new("IntValue")
value.Name = i
value.Value = v
value.Parent = materialFolder
table.insert(plrStats, v)
end
-----------------------------------------------------------------
local currencyTable = plrStats.currency
local currencyFolder = Instance.new("Folder")
currencyFolder.Name = "Currencies"
currencyFolder.Parent = playerFolder
for i,v in pairs(currencyTable) do
print("'"..i.."'","= '"..v.."'")
local value = Instance.new("IntValue")
value.Name = i
value.Value = v
value.Parent = currencyFolder
table.insert(plrStats, v)
end
-----------------------------------------------------------------
local customTable = plrStats.customs
local customsFolder = Instance.new("Folder")
customsFolder.Name = "Customs"
customsFolder.Parent = playerFolder
for i,v in pairs(customTable) do
print("'"..i.."'","= '"..v.."'")
local value = Instance.new("IntValue")
value.Name = i
value.Value = v
value.Parent = customsFolder
table.insert(plrStats, v)
end
-----------------------------------------------------------------
local baseTable = plrStats.baseStats
local baseFolder = Instance.new("Folder")
baseFolder.Name = "Base Stats"
baseFolder.Parent = playerFolder
for i,v in pairs(baseTable) do
print("'"..i.."'","= '"..v.."'")
local value = Instance.new("IntValue")
value.Name = i
value.Value = v
value.Parent = baseFolder
table.insert(plrStats, v)
end
-----------------------------------------------------------------
local addTable = plrStats.addStats
local addFolder = Instance.new("Folder")
addFolder.Name = "Additional Stats"
addFolder.Parent = playerFolder
for i,v in pairs(addTable) do
print("'"..i.."'","= '"..v.."'")
local value = Instance.new("IntValue")
value.Name = i
value.Value = v
value.Parent = addFolder
table.insert(plrStats, v)
end
print(plrStats)
-----------------------------------------------------------------
end)
function Save(player)
local success, errormessage = pcall(function()
myDataStore:SetAsync(player.UserId.."-DATA", plrStats)
end)
if success then
print("Data Successfully Saved.")
else
warn("There was an Error Saving Data",errormessage)
end
end
game.Players.PlayerRemoving:Connect(function(player)
Save(player)
end)
game:BindToClose(function()
for i, player in pairs(game.Players:GetPlayers()) do
Save(player)
end
end)
UpdateAsync() doesn’t use the same syntax of SetAsync()