Basically, I’m making a quest system with tables, But when I try and add a new quest to the pool of quests, It doesn’t add it, Until I change the data key, However When I change the data key, It wipes existing data. Is there a way to circumvent this?
local function QuestsData()
return {
Play3Maps = {
Value = false;
CodeName = "PLAY3MAPS";
InProgress = false;
NameQuest = "Play 3 Maps!";
Description = "Play any 3 maps in game for a reward.";
RewardInCoins = nil;
RewardInXP = 100
};
Walk100Studs = {
Value = false;
CodeName = "Walk100Studs";
InProgress = false;
NameQuest = "Walk 100 Studs";
Description = "Walk 100 studs in any given direction.";
RewardInCoins = 25;
RewardInXP = nil
};
}
end
local function QuestsData()
return {
Play3Maps = {
Value = false;
CodeName = "PLAY3MAPS";
InProgress = false;
NameQuest = "Play 3 Maps!";
Description = "Play any 3 maps in game for a reward.";
RewardInCoins = nil;
RewardInXP = 100
};
Walk100Studs = {
Value = false;
CodeName = "Walk100Studs";
InProgress = false;
NameQuest = "Walk 100 Studs";
Description = "Walk 100 studs in any given direction.";
RewardInCoins = 25;
RewardInXP = nil
};
}
end
local function loadData(p)
local data
local Success, Error = pcall(function() data = PlayerData:GetAsync("data:"..p.UserId)
end)
if not Success then
warn("Could not get data for "..p.Name)
return
end
if not data then
data = QuestsData()
end
ServerData[p] = data
end
local function saveData(p)
if not SavingFor[p] then
SavingFor[p] = true
if ServerData[p] then
local Success, Error = pcall(function()
PlayerData:SetAsync("data:"..p.UserId, ServerData[p])
end)
if not Success then
warn("Data wasn't saved for "..p.Name..".")
return
end
ServerData[p] = nil
end
SavingFor[p] = nil
end
end```
Anything data related is like runes to me. anything outside of datastore im fine at but I have zilch knowledge on data handling, Perhaps could you provide an example?
if not data then
data = QuestsData()
else
local template = QuestsData()
for _, t in pairs(template) do
if not table.find(data, t) then
table.insert(data, t)
end
end
end
Not the biggest expert with data handling either, so you should probably take my advice with a grain of salt.
Ok it’s very weird, I didn’t change the datakey BUT It now does show the results without needing the datakey, But it just resets it to false and the original value.
No no no, Whenever I try and add a new table to the pool, It does show that it was added without needing to change the datakey, But wipes the old data. And sets it to its template
Ah, I see my mistake. Try and change it to this:
The last example I gave checked if the entire tables were the same, and since some values were different, it changed them anyway. This one should just check whether it can find a table with the same CodeName.
if not data then
data = QuestsData()
else
local template = QuestsData()
for _, t in pairs(template) do
local found = false
for _, t2 in pairs(data) do
if t.CodeName == t2.CodeName then
found = true
end
end
if not found then
table.insert(data, t)
end
end
end