What do you want to achieve? I want to get my datastore2 script working.
What is the issue?
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")
local DataStore2 = require(game.ServerScriptService.DataStore2)
DataStore2.Combine("DATA", "Stats", "Inventory")
local Stats = {
Strength = 0;
Rebirths = 0;
Cash = 1000;
Speed = 20
}
local Inventory = {
Cat = false;
BigGear = false
}
local playersavetable = {};
local function loadStarterData(Player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = Player
local Inv = Instance.new("Folder")
Inv.Name = "Inventory"
Inv.Parent = leaderstats
for statname, statvalue in pairs(Stats) do
if type(statvalue) == 'number' then
local intvalue = Instance.new("IntValue")
intvalue.Name = statname
intvalue.Value = statvalue
intvalue.Parent = leaderstats
end
for statname, statvalue in pairs(Inventory) do
if type(statvalue) == 'boolean' then
local intvalue = Instance.new("BoolValue")
intvalue.Name = statname
intvalue.Value = statvalue
intvalue.Parent = Inv
end
end
end
end
local function loadData(player)
local Data
local s, e = pcall(function()
Data = DataStore2:GetAsync('UserId'..player.UserId)
end)
if s then
print (player.Name.."Data loaded")
else
print(player.Name.."Data failed to load")
end
if Data then
for statname, statvalue in pairs(Data) do
if type(statvalue) == "number" then
player.leaderstats[statname].Value = statvalue
else if type(statvalue) == "boolean" then
player.leaderstats.Inventory[statname].Value = statvalue
end
end
end
print(player.Name.."Data has been loaded")
else
print(player.Name.."No data found! generating..")
end
end
local function saveData(player, Stats)
--if RunService:IsStudio() then return end
local Data = {}
for _, stat in ipairs(Stats:GetChildren()) do
if not stat:IsA("Folder") then
Data[stat.Name] = stat.Value
end
end
local s, e = pcall(function()
DataStore2:SetAsync('UserId'..player.UserId, Data)
end)
if s then
print(player.Name.."Data has been saved")
else
warn (player.Name.."Data failed to save"..e)
end
end
ReplicatedStorage.SaveEvent.OnServerEvent:Connect(function(player)
saveData(player, player.leaderstats)
end)
Players.PlayerAdded:Connect(function(player)
playersavetable[player] = tick()
loadStarterData(player)
loadData(player)
end)
Players.PlayerRemoving:Connect(function(player)
saveData(player, player.leaderstats)
end)
An error it is outputting is " [ TyDye_RBLXData failed to saveServerScriptService.datastore:84: attempt to call method âSetAsyncâ (a nil value)]"
There are two issues, the data is not saving and everything in the inventory folder is duplicated 4 times.
What solutions have you tried so far? MANY THINGS! I have studio datastore API enabled, I have the datastore2 âSaveInStudioâ boolvalue. I have also been working on this script for nearly an hour and have made little to no progress. I have looked on the datastore2 documentation but that helped me little to none.
local function saveData(player, Stats)
--if RunService:IsStudio() then return end
local Data = {}
----------- for _, stat in ipairs(Stats:GetChildren()) do
if not stat:IsA("Folder") then
Data[stat.Name] = stat.Value
end
end
local s, e = pcall(function()
DataStore2:SetAsync('UserId'..player.UserId, Data)
end)
if s then
print(player.Name.."Data has been saved")
else
warn (player.Name.."Data failed to save"..e)
end
end
Line 78 is the 2nd line I have commented out in this reply (It is not commented in the actual script)
Ahhh! Is it even possible todo this with two separate tables or am I just missing something? Please note that this used to be a normal datastore script but I am attempting to make it datastore2 and add a separate table
Both times in the Script you call saveData() with the Player but without the Stats.
You can fix this by either calling saveData like, saveData(player, player.leaderstats) or by editing the saveData function to call GetChildren on player.leaderstats, player.leaderstats:GetChildren().
Your not the only one who has this struggle. Recently Iâve been trying a lot of different things, even resorting to the toolbox, but I can never really save so much as leaderstats. I mean you can try AlvinBloxâs tutorial on DataStores, but that hasnât really helped me since I want to work with 2 values, not just one. I have a hard time with DataStores, and I think a lot of people do. Maybe when this getâs resolved I will be able to learn a thing or two. For now, I have no real suggestions other than look at AlvinBloxâs tutorial, but then again, he only uses one value.
Hooray! We fixed a issue! but now I get this output " [ TyDye_RBLXData failed to saveServerScriptService.datastore:84: attempt to call method âSetAsyncâ (a nil value)]"
EDIT: Updated OP to make it easier for others to help.
Looking at the code again, youâre trying to use DataStore2 just like normal DataStores which is not the case.
DataStore2 has indivdual DataStores for each player which you open via DataStore2(Player, "DataStoreName"), these objects have methods such as: :Get(), :Set(), etc that do not take Keys.
local function loadData(player)
local Data
local s, e = pcall(function()
DataStoreService:GetDataStore("Stats"):GetAsync(player.UserId)
end)
if s then
print (player.Name.."Data loaded")
else
warn (player.Name.."Data failed to load".. e)
end
if Data then
for statname, statvalue in pairs(Data) do
if type(statvalue) == "number" then
player.leaderstats[statname].Value = statvalue
else if type(statvalue) == "boolean" then
player.leaderstats.Inventory[statname].Value = statvalue
end
end
end
print(player.Name.."Data has been loaded")
else
print(player.Name.."No data found! generating..")
end
end
local function saveData(player, Stats)
--if RunService:IsStudio() then return end
local Data = {}
for _, stat in ipairs(Stats:GetChildren()) do
if not stat:IsA("Folder") then
Data[stat.Name] = stat.Value
end
end
local s, e = pcall(function()
DataStoreService:GetDataStore("Stats"):GetAsync(player.UserId)
end)
if s then
print(player.Name.."Data has been saved")
else
warn (player.Name.."Data failed to save"..e)
end
end
It took the errors away but when I join the game It prints my data has been loaded and instantly after prints that no data was found generating.
You never define Data as what GetAsync returns, furthermore Data is never going to be loaded.
FYI: Just because the pcall succeeds does not mean the GetAsync returned anything.
Also, incide the saveData pcall you used GetAsync, you should be changing that to SetAsync with the arguments of the Key and the Data.