Hello!
I'm more or less looking for areas of improvement on my script, or potentially a solution as to why the datastore isn't working in studio, but works in game. It seems to be an issue with saving since data does load when in studio.
Near the end, where the DS:SetAsync is, seems to be the issue. I added a few prints yesterday and it seems to halt as the DS:SetAsync happens, as well as the print at the end not happening, so there may be an issue with my code. I was thinking maybe it was an issue with what was going in, the table to be more specific. Anyway, I'm open to suggestions. No errors are outputted.
local DS = game:GetService("DataStoreService"):GetDataStore("yourdatastore", "testing1")
game.Players.PlayerAdded:Connect(function(player)
local variablePlayer = player.Name
local folder = Instance.new("Folder",player)
folder.Name = "inventoryfolder"
local dataKey = "userkey_"..player.UserId
local data
local success,errormsg = pcall(function()
data = DS:GetAsync(dataKey)
end)
if success then
if data ~= nil then
print("Player has data.")
for name,value in pairs(data) do
local i2 = Instance.new("IntValue", folder)
i2.Name = name
i2.Value = value
print(name, value)
end
else
print("Player is new.")
local defaultvalue = Instance.new("IntValue", folder)
defaultvalue.Name = "Default Value"
defaultvalue.Value = 7 * math.random(4, 17)
end
else
warn("Error when retrieving data for "..player.Name..". Error: "..errormsg)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local inventory1 = {}
for i, v in pairs(player.inventoryfolder:GetChildren()) do
inventory1[v.Name] = v.Value -- add the value to the table
print(v.Name, v.Value)
end
local success,errormsg = pcall(function()
DS:SetAsync("userkey_"..player.UserId, inventory1)
end)
if success then
print("Successfully saved for "..player.Name)
else
warn("Unable to save for "..player.Name..". Error: "..errormsg)
end
end)

1 comment
I had a single value save. Maybe this is different? This script is based on the single value script.