Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Trying to save a table and then load values into model?

Hi guys! In the player, I create a model with different values in them. Instead of individually saving each value, I am trying to put all the values into a table, save that and then load that back into the player when they join the game. I am not 100% about this, and it is not working :/ Any ideas? Thank you very much :) PS: I want to save it as the values change, because if you do it on "PlayerRemoving", sometimes the player can crash and it wont save!

game.Players.PlayerAdded:connect(function(Player)
model = Instance.new("Model")
model.Name = "PlayerModel"
model.Parent = Player

local value = Instance.new("BoolValue")
value.Name = "ValueOne"
value.Parent = model

local lol = Instance.new("IntValue")
lol.Name = "ValueTwo"
value.Parent = model

local dataStore = game:GetService("DataStoreService"):GetDataStore("PlayerModel")
local tab = dataStore:GetAsync("PlayerModel")

for _, child in pairs(model:GetChildren()) do 
    for _, saved in pairs(tab) do

        if rawequal(saved.Name, child.Name) then

            child.Value = saved.Value
        end
    end
end
end)


local dataStore = game:GetService("DataStoreService"):GetDataStore("PlayerModel") 
local tab = {}

for _, v in pairs(model:GetChildren()) do
    tab[#tab + 1] = {Name = v.Name, Value = v.Value}
  
end

dataStore:SetAsync("PlayerModel", tab)

1 answer

✓ Accepted answer
local dataStore = game:GetService("DataStoreService"):GetDataStore("PlayerModel")
local utility = game:GetService("HttpService")

game.Players.PlayerAdded:connect(function(player)
    local model = Instance.new("Model")
    model.Name = "PlayerModel"
    model.Parent = player

    local value = Instance.new("BoolValue")
    value.Name = "ValueOne"
    value.Parent = model

    local lol = Instance.new("IntValue")
    lol.Name = "ValueTwo"
    lol.Parent = model

    local tab = dataStore:GetAsync(player.userId) or {}

    for _, child in pairs(model:GetChildren()) do 
        for _, saved in pairs(utility:JSONDecode(tab)) do
            if rawequal(saved.Name, child.Name) then
                child.Value = saved.Value
            end
        end

        child.Changed:connect(function(property)
			local tab = {}
			       
            for _, v in pairs(child.Parent:GetChildren()) do
                tab[#tab + 1] = {Name = v.Name, Value = v.Value}
            end

            local encoded = utility:JSONEncode(tab)
            dataStore:SetAsync(player.userId, encoded)              
        end)
    end
end)

game.OnClose = function()
    wait(2)
end

18 comments

0

Thanks for helping! I have edited my post to what I now have! It is still not working though :( Any help would be greatly appreciated!

0

Data stores are down at the moment, could be the reason why.. I'll take a look, though.

0

OK! Thanks! :)

0

OK, thanks for helping although I get these errors :( Bad argument #1 to pairs(table expected, got nil) RbxUtil.DecodeJson expects string argument . RbxUtil.DecodeJson is Deprecated Any ideas? Thanks

0

I fixed it by setting tab to the returned table or {}

0

Thanks so much, but I still get the same errors :(

1

Whatttt! DecodeJSon..(table expected, got nil)??

0

Yeah :/

1

finally works! the Utility library only encoded/decoded strings, while HTTP service's actually works with tables.

0

Thanks! :) let me go and test it..! :D

0

Thank you so much! It works! :D I just have one more question if you don't mind (I really appreciate all this!) - instead of saving it at gameclose(might not save it the game crashed)... how could I save it each time one of the values changed? (If this is not possible, is there a better way to do this?) Thanks!

1

Saving it each time a value is changed will most-likely exceed the request limit (depending on how frequently the value changes). If you aren't changing the value too often.. yeah, you can do that.

0

Well, I am not saving it too often, so it should be fine! :) Do you mind doing that for me? I would really appreciate it! :P Thank you so much for all your help!!

1

ok

0

Thanks!! :)

0

Hmm :( Line 18 and 32 are erroring! :( Any ideas? Thanks!

0

I once again fixed it. No more edits. Ask another question if you must.

1

Thank you so much for all your help! :) I really appreciate it!

This discussion is preserved as it was. New answers, comments, and votes are closed.