I’m busy with creating an system what only use one table and so on one data store and only save once on player leave but I come up to an error where it doesn’t set the default value on loading when data doesn’t exist
Also I just want it to loop trough the entire table and also check if the same exist into the data store and just keep the default table values when not yet exist
So template be set into the user id into the player data
Then I want it to loop trough the entire table and set valeus to the data store valeus elseif nil skip it and keep defould
But how I go about it with nested tables this is needed for if there’s new data
So now I do it like this but I want to load the entire table with the datastore and use default valued if it won’t exist
local sesionData = require(script.Parent.SesionData)
local DS = game:GetService("DataStoreService")
local PlrData = DS:GetDataStore("PlayerData_-1")
local Config = script.Parent.Configuration
local Tools = {
[1] = "TestTool",
[2] = "TestTool2",
}
local module = {
load = function(userId)
local data = PlrData:GetAsync(userId)
if data == nil then
PlrData:SetAsync(userId, sesionData.Template)
end
task.wait(0.1)
local SavedData = PlrData:GetAsync(userId)
sesionData.PlayerData[userId] = sesionData.Template
warn(sesionData.PlayerData)
task.wait(0.1)
local inventory = sesionData.PlayerData[userId].Inventory
warn(inventory)
task.wait(0.1)
for i = 1, Config:GetAttribute("AmounthTools") do
local toolName = Tools[i]
warn(toolName)
if not SavedData.Inventory[toolName].Owned then
-- Initialize with a default structure if it doesn't exist
SavedData.Inventory[toolName].Owned = false
end
inventory[toolName].Owned = SavedData.Inventory[toolName].Owned
print(sesionData.PlayerData[userId])
end
end,
}
return module
The code you provided has the following errors and issues:
script.Parent is not defined anywhere in the script. Make sure to define it before using it.
In the loop, the Tools table indices are starting from 1 instead of 0. To fix this, update the loop condition to for i = 1, Config:GetAttribute("AmounthTools") do and change the Tools[i] reference to Tools[#Tools + 1 - i].
The Config variable is not defined. Make sure to define it before using it.
Instead, use
local SesionData = require(script.Parent.SesionData)
local DS = game:GetService("DataStoreService")
local PlrData = DS:GetDataStore("PlayerData_-1")
local Config = script.Parent.Configuration
local Tools = {
[#Tools] = "TestTool",
[#Tools + 1] = "TestTool2",
}
local module = {
load = function(userId)
local data = PlrData:GetAsync(userId)
if data == nil then
PlrData:SetAsync(userId, SesionData.Template)
end
task.wait(0.1)
local SavedData = PlrData:GetAsync(userId)
SesionData.PlayerData[userId] = SesionData.Template
warn(SesionData.PlayerData)
task.wait(0.1)
local inventory = SesionData.PlayerData[userId].Inventory
warn(inventory)
task.wait(0.1)
for i = #Tools, 1, -1 do
local toolName = Tools[i]
warn(toolName)
if not SavedData.Inventory[toolName].Owned then
-- Initialize with a default structure if it doesn't exist
SavedData.Inventory[toolName].Owned = false
end
inventory[toolName].Owned = SavedData.Inventory[toolName].Owned
print(SesionData.PlayerData[userId])
end
end,
}
return module
for key, value in Config do
-- Key is whatever the thing is called (in this case it will just be numbers)
-- Value is the value associated with the key
end
Do you want all leaf elements to be replaced or only down to a specific level? So if you load a table with Eggs that have a Box entry but no Prize, do you want to add the default Prize or just leave it?
This will add any items not in “loaded” with the same chain of indices from “defaults”
local defaults = {
Food = {
Eggs = {
Box = "cardboard",
Prize = 50,
},
Chocolate = {
Box = "paper",
Prize = 60,
}
}
}
local loaded = {
Food = {
Eggs = {
Box = "plastic",
Prize = 20
}
},
Slime = {
Green = 20
}
}
--Ensure table has all the indices in a defaults table
--If not, copy them from the defaults
function SupplyDefaults(loaded, defaults)
for i, v in pairs(defaults) do
local has = loaded[i]
if type(v) == "table" then
if not has then
loaded[i] = {}
end
--Check sub-table
SupplyDefaults(loaded[i], v)
elseif not has then
loaded[i] = defaults[i]
else
--Already has this value
end
end
return loaded
end
loadedwdefault = SupplyDefaults(loaded, defaults)