I am trying how to do datastore but when I do what others do, the saving system keeps telling me that data is not saved. For about 30 times. I think that is not normal. So, I want to know what’s wrong with my script.
local DataStore = game:GetService("DataStoreService")
local SaveEvent = game.Workspace.Events.SaveData
local LoadEvent = game.Workspace.Events.LoadData
local DiedEvent = game.Workspace.Events.PlayerDied
local ResetData = game.Workspace.Events.ResetData
local PlayerGameDatas = game.ServerStorage.PlayerDatas
local Data = DataStore:GetDataStore("DATA")
function SaveData(Player) --- Overwrite a new save ---
local UnsavedData = PlayerGameDatas:WaitForChild(Player.Name)
repeat
wait(1)
local Success,ErrMsg = pcall(function()
Data:SetAsync("Player_"..Player.UserId,UnsavedData)
end)
if Success == true then
print("Pro")
else
print("NotPro")
end
until Success
end
function LoadData(Player) --- Load Saves from datastore ---
local PlayerData = Data:GetAsync("Player_"..Player.UserId)
if PlayerData then
PlayerData.Parent = PlayerGameDatas
print("Save Loaded")
else
local Structure = script.DataStructure:Clone()
Structure.Parent = PlayerGameDatas
Structure.Name = Player.Name
print("New Save Created")
end
end
function ResetData(Player) --- Set everything to default ---
end
SaveEvent.OnServerEvent:Connect(function(Plr)
SaveData(Plr)
end)
LoadEvent.OnServerEvent:Connect(function(Plr)
LoadData(Plr)
end)
You have a few problems. The biggest is probably that your trying to save lua userdata. Any roblox class object; numberValue, stringValue, part, model, etc. is userdata.
And all data in roblox datastores are saved as strings in JSON format. Attempting to encode any userdata as a JSON string will return ‘null’. So if you’re not getting an error, you’re just saving nothing.
type(Instance.new('NumberValue')) -- userdata; Cannot be saved
type({stats = {}, points=100 }) -- table; Can be saved
That’s a good idea for sure. Although it doesn’t really work out. You have a table which can be saved, but you’re still relying on userdata in some way. So this in JSON string format, would look like- {“points”:null}. The table gets saved, but the userdata is lost.
You’ll want to have some conversion from numberValues, stringValues, folders, etc to lua datatypes.
Here's some functions to help with converting the conversions.
local convertToTable
convertToTable = function(robloxObject)
local tab={}
for _,child in pairs(robloxObject:GetChildren()) do
if child:IsA('Folder') or child:IsA('Model') then
tab[child.Name] = convertToTable(child)
elseif child:IsA('ValueBase') then
tab[child.Name] = child.Value
end
end
return tab
end
local convertToRobloxObject
convertToRobloxObject = function (luaType)
local instance
if type(luaType)=='table' then
instance=Instance.new('Folder')
for name, value in pairs(luaType) do
local entry = convertToRobloxObject(value)
entry.Name=name
entry.Parent=instance
end
elseif type(luaType)=='number' then
instance=Instance.new('NumberValue')
instance.Value=luaType
elseif type(luaType)=='boolean' then
instance=Instance.new('BoolValue')
instance.Value=luaType
elseif type(luaType)=='string' then
instance=Instance.new('StringValue')
instance.Value=luaType
end
return instance
end
In the end, you might find it easier to only rely on lua tables for data. But hope this helps.
The first thing I’d do when I receive an error and can’t figure it out, is search that exact error message on google. It’s a good way to go about things. Hope I was of some help.