Kampfkarren / Roblox

Scripts and stuff I wrote for Roblox. Documentation is little to none as these are just stuff I took from my game that I thought I could share.
https://kampfkarren.github.io/Roblox/
Other
284 stars 78 forks source link

Update GetTableAsync to patch tables inside the value #102

Closed Scyfren closed 4 years ago

Scyfren commented 4 years ago

Currently, DataStore:GetTableAsync(default) only patches missing keys on the first "layer" of the DataStore value. If the value itself contains tables, it will not sync with the respective default value.

e.g. If the value looks like this:

{
    Equipped = "Default",
    Owned = {
        Backpack = true
    }
}

and default looks like this:

{
    Equipped = "Default",
    Owned = {
        Default = true
    }
}

Owned.Default = true will not be added to the DataStore value.

Solution: TableUtil.sync(tbl, default) -> bool Adds missing keys to tbl from default. If tbl contains table values, TableUtil.sync will be used on those table values. Returns whether tbl was changed at all.

When DataStore:GetTableAsync(default) is called, it should now sync the DataStore value with default, where if the value itself contains a table, it will sync that table with the respective table value in default.

Kampfkarren commented 4 years ago

This is backwards incompatible behavior, it doesn't do this now intentionally in the case of something like setting an inventory table, and wanting to change the default items later, or changing the schema on how items are handled.

I may be open to this as a different function though.