Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 8 years ago

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

Log in to vote
1
Answered by
ImageLabel 1541 Moderation Voter
8 years ago
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
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! jjwood1600 215 — 8y
0
Data stores are down at the moment, could be the reason why.. I'll take a look, though. ImageLabel 1541 — 8y
0
OK! Thanks! :) jjwood1600 215 — 8y
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 jjwood1600 215 — 8y
View all comments (14 more)
0
I fixed it by setting tab to the returned table or {} ImageLabel 1541 — 8y
0
Thanks so much, but I still get the same errors :( jjwood1600 215 — 8y
1
Whatttt! DecodeJSon..(table expected, got nil)?? ImageLabel 1541 — 8y
0
Yeah :/ jjwood1600 215 — 8y
1
finally works! the Utility library only encoded/decoded strings, while HTTP service's actually works with tables. ImageLabel 1541 — 8y
0
Thanks! :) let me go and test it..! :D jjwood1600 215 — 8y
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! jjwood1600 215 — 8y
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. ImageLabel 1541 — 8y
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!! jjwood1600 215 — 8y
1
ok ImageLabel 1541 — 8y
0
Thanks!! :) jjwood1600 215 — 8y
0
Hmm :( Line 18 and 32 are erroring! :( Any ideas? Thanks! jjwood1600 215 — 8y
0
I once again fixed it. No more edits. Ask another question if you must. ImageLabel 1541 — 8y
1
Thank you so much for all your help! :) I really appreciate it! jjwood1600 215 — 8y
Ad

Answer this question