Hey Developers!
I’ve recently scripted a datastore that is supposed to save my custom inventory values in a table, and a table of the clothes and accessories the player wore.
The loading works perfectly fine, but the saving works only in studio.
There’s no use of RunService:IsStudio() and I added a game:BindToClose() that saves the data.
I used PCalls, and tried to print the errors when friends triggered the save prints, and I’ve tried to add a retry that re-runs the SetAsync if fails. And I’ve tried to look onto Discord whether the data was saved or not, and it ran only in Studio.
I’ve also tried to use UpdateAsync for both loading and saving for it to respect the queue.
I couldn’t get it to work.
I’ve used the search bar and tried the following solutions:
And a few other ones that don’t have a marked solution.
My code:
game:GetService("Players").PlayerRemoving:Connect(function(player) --When the player is leaving do the following commands
local inventory = player:WaitForChild("Clothes") --Gets the inventory folder
local userId = player.UserId --Gets the player's userId
local inv = {} --Createa new table
for i,child in pairs(inventory:GetChildren()) do --For everything in the inventory do:
inv[child.Name] = {child.Value, child:GetAttribute("Type")} --Create a new place in the inventory table and add the value to it
print(child.Value) --Print the value, to make sure it is not nil
-------------------------This is printed.
end
local char = player.Character or player.CharacterAdded:Wait()
local worn = {
["Accessories"] = {
["Hat"] = char:FindFirstChild("GameHat"),
["Hair"] = char:FindFirstChild("GameHair"),
["Accessory"] = char:FindFirstChild("GameAccessory"),
},
["Clothing"] = {
["Shirt"] = char:FindFirstChild("Shirt"),
["Pants"] = char:FindFirstChild("Pants")
},
["HatsToggled"] = char:GetAttribute("HatsToggled")
}
for i,v in pairs(worn["Accessories"]) do
if(v) then
worn["Accessories"][i] = v:GetAttribute(v.Name:gsub("Game", "") .. "Name")
end
end
for i,v in pairs(worn["Clothing"]) do
if(v) then
worn["Clothing"][i] = v:GetAttribute("AssetId")
end
end
local success,err
local tries = 0
print("Before save")
--------------------------------This isn't print either for some reason
repeat
tries+=1
success, err = pcall(function() --Create a function that returns or an error or a success
dataStore:UpdateAsync("-Clothes" .. player.UserId, function(oldData)
print("Data is being saved") --This is not being printed as well.
return {inv, worn} --Set the player's data to the new table of his things.
end)
end)
if(success) then --If it worked
print("Saved data!") --Print Saved Data!
elseif(err) then --Else if there's an error
print("there's an error saving data") --print to the player that there was an error
warn(err) --Warn it
----------------------------------------Nothing is printed ^
wait(1)
end
until tries == 1 or success == true
print("After save")
print(tries, success, err)
end)
game:BindToClose(function()
if(game:GetService("RunService"):IsStudio()) then
wait(4)
else
for i,v in pairs(game:GetService("Players"):GetPlayers()) do
v:Kick("Game Shut-Down, saving data...") --Run the PlayerRemoving function
end
end
end)
@tozzleboy Yes I enabled the studio to API services