My dataStore script doesn’t seem to be working. No errors are shown in the output. I’m not sure if the data saving part is not working or if the GetAsync part isn’t working. The print(“Player has data”) does not print and the data isn’t loaded in. Thanks.
local ds = game:GetService("DataStoreService"):GetDataStore("CardSaving")
local cards_folder = game.ServerStorage.Cards
game.Players.PlayerAdded:Connect(function(plr)
local cards_fold = Instance.new("Folder")
cards_fold.Name = "Cards"
cards_fold.Parent = plr
for _, v in pairs (cards_folder:GetChildren()) do
local card = Instance.new("Folder")
card.Name = v.Name
card.Parent = cards_fold
local cardOwned = Instance.new("BoolValue")
cardOwned.Name = "CardOwned"
cardOwned.Parent = card
local cardAmount = Instance.new("IntValue")
cardAmount.Name = "CardAmount"
cardAmount.Parent = card
local lvl = Instance.new("IntValue")
lvl.Name = "Level"
lvl.Parent = card
if ds:GetAsync(cards_fold.Parent.UserId .. plr.UserId) ~= nil then
print("Player has data")
local succ, msg = pcall(function()
cardOwned.Value = ds:GetAsync("card".. plr.UserId)[2]
cardAmount.Value = ds:GetAsync("card".. plr.UserId)[2]
lvl.Value = ds:GetAsync("card".. plr.UserId)[2]
end)
if not succ then
warn("Problem with getting and setting data "..msg)
end
end
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local succ, msg = pcall(function()
local data = {}
for _, v in pairs (plr.Cards:GetChildren()) do
local card = v
for _, v in pairs (card:GetChildren()) do
table.insert(data,{v.Name, v.Value})
end
end
ds:SetAsync(plr.UserId, data)
end)
if not succ then
warn("Problem with saving data ".. msg)
end
end)
“cards_fold.Parent.UserId … plr.UserId”
The data that you save is saved as “UserId,” not “UserIdUserId.”
1 Like
You’re setting and getting different keys.
Change ds:GetAsync(cards_fold.Parent.UserId .. plr.UserId) to ds:GetAsync(plr.UserId)
1 Like
I changed it to that and the print works, but the data still doesn’t load in for some reason. Also print(“Player has data”) only prints 3 times even though there are 4 children in cards_folder. Could that be a problem?
Your data isn’t loading properly because you’re not handling it properly. When you load the data in, you’re trying to load the key "card"..plr.UserId, but you never save anything using that key. The way you’re saving it is by saving a 2D table under the player’s UserId alone.
So when you load in the data, assign ds:GetAsync(plr.UserId) to a variable, and then access the player data through that variable instead of lines like ds:GetAsync("card".. plr.UserId)[2]
Does that make any more sense?
2 Likes
Like this???
local data = ds:GetAsync(plr.UserId)
if data ~= nil then
local succ, msg = pcall(function()
print("Player has data")
cardOwned.Value = ds:GetAsync(data)
cardAmount.Value = ds:GetAsync(data)
lvl.Value = ds:GetAsync(data)
end)
if not succ then
warn("Problem with getting and setting data "..msg)
end
end
end
You’re getting there. You don’t need to use GetAsync() after creating the data variable. From there, you’re getting a table of all the player’s data.
Based on the way you’re saving things, your table looks like this:
data = {
{"CardOwned", false},
{"CardAmount", 100},
{"Level", 2},
{"CardOwned", false},
{"CardAmount", 200},
{"Level", 4},
...
}
So you still have a couple problems. First, you’re not saving the values under cards, you’re just saving all the card properties in no particular order in a long list. Second, you need to access the data through the data variable instead of trying to use GetAsync().
1 Like
I tried to do this, but I keep getting an error
Problem with saving data ServerScriptService.SavingPlayerCardsValue:68: invalid argument #1 to ‘insert’ (table expected, got string)
Could doing something like this work?
game.Players.PlayerRemoving:Connect(function(plr)
local succ, msg = pcall(function()
local Chloe = {}
local KEKI = {}
local Radley = {}
local Sabaku = {}
for _, v in pairs (plr.Cards:GetChildren()) do
local card = v
for _, v in pairs (card:GetChildren()) do
table.insert(card.Name,{v.Name, v.Value})
end
end
ds:SetAsync("Chloe"..plr.UserId, Chloe)
ds:SetAsync("KEKI"..plr.UserId, KEKI)
ds:SetAsync("Radley"..plr.UserId, Radley)
ds:SetAsync("Sabaku"..plr.UserId, Sabaku)
end)
if not succ then
warn("Problem with saving data ".. msg)
end
end)
Thanks
The error you’re referencing means that you’re trying to insert a value without specifying which table you want to put the value in.
See “table.insert” in table | Documentation - Roblox Creator Hub
Basically, you would do something like table.insert(data, "someValue") to insert "someValue" into the data table.
Now you could do what you’re suggesting there, it’s just not procedural/adaptive and could get quite cumbersome if you decide to add a ton of cards. If you only plan to have a few, then that would work fine. Just remember to load it in the same manner that you’re saving it.
1 Like
Shouldn’t this work then?
local Sabaku = ds:GetAsync("Sabaku".. plr.UserId)
if v.Name == "Sabaku" and Sabaku ~= nil then
local succ, msg = pcall(function()
cardOwned.Value = (Sabaku)
cardAmount.Value = (Sabaku)
lvl.Value = (Sabaku)
end)
if not succ then
warn("Problem with getting and setting data "..msg)
end
end
game.Players.PlayerRemoving:Connect(function(plr)
local succ, msg = pcall(function()
for _, v in pairs (plr.Cards:GetChildren()) do
local card = v
for _, v in pairs (card:GetChildren()) do
if card.Name == "Sabaku" then
table.insert(Sabaku, {v.Name, v.Value})
end
end
end
end)
ds:SetAsync("Sabaku"..plr.UserId, Sabaku)
end)