I been using this code to save values in my game, it worked perfectly well for months but it was until this few days when I was working in the studio this bug happened to me twice while I busy testing.
This is odd as it never happened until these few days, perhaps is this not related to the code/script itself and it’s the fault of the internal server whatever?
local dataStoreService = game:GetService("DataStoreService")
local serverStorage = game:GetService("ServerStorage")
local saveFileStore = dataStoreService:GetDataStore("SaveFileStore")
local Players = game:GetService("Players")
--[[-----------------------------------------------------
------------------------Functions------------------------
--]]-----------------------------------------------------
--Setup values for newbies
local function CreateValues(file)
for _, value in pairs(script.Values:GetChildren()) do
if not file:FindFirstChild(value.Name) then
local valueClone = value:Clone()
valueClone.Parent = file
end
end
end
local function TablizeObjects(array,file)
for i, object in pairs(file:GetChildren()) do
local objectName = object.Name
if object:IsA("ValueBase") then
array[objectName] = {
["ValueType"] = object.ClassName,
["ValueName"] = objectName,
["ValueValue"] = object.Value
}
end
end
end
--[[-----------------------------------------------------
----------------------Save the file----------------------
--]]-----------------------------------------------------
Players.PlayerRemoving:Connect(function(player)
local file = player:WaitForChild("SaveFile")
local playerUserID = player.UserId
local saveFileArray = {}
TablizeObjects(saveFileArray,file) --Create array
local setSuccess, errorMessage = pcall(function()
saveFileStore:SetAsync(playerUserID, saveFileArray) --Save array
end)
if not setSuccess then
warn(errorMessage)
end
end)
--[[-----------------------------------------------------
----------------------Load the file----------------------
--]]-----------------------------------------------------
Players.PlayerAdded:Connect(function(player)
local playerUserID = player.UserId
local getSuccess, loadSaveFile = pcall(function()
return saveFileStore:GetAsync(playerUserID) --Pull store data
end)
local file = Instance.new("Folder", player) --Create SaveFile folder
file.Name = "SaveFile"
if getSuccess and loadSaveFile then
--Start adding in the folder contents
for i, object in pairs(loadSaveFile) do
local valueType = object["ValueType"]
local valueInstance = Instance.new(valueType)
valueInstance.Parent = file
valueInstance.Value = object["ValueValue"]
valueInstance.Name = object["ValueName"]
end
end
CreateValues(file)
end)