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

DataStore will not save the items in my table, what did I do wrong?

Asked by 3 years ago
Edited 3 years ago

So I'm working on a skin shop in my game. But for some reason my Data Store will not save my table that I've created with the object models of the skins. Here's my error message

DataStore Error: 35: invalid argument #1 to 'pairs' (table expected, got string)

local dataStoreService = game:GetService("DataStoreService")
local TokensDataStore = dataStoreService:GetDataStore("Tokens")
local WinsDataStore = dataStoreService:GetDataStore("Wins")
local InventoryDataStore = dataStoreService:GetDataStore("Inventory")
local http = game:GetService("HttpService")
game.Players.PlayerAdded:Connect(function(plr)
    local leaderstats = Instance.new("Folder",plr)
    leaderstats.Name = "leaderstats"
    local Wins = Instance.new("IntValue",leaderstats)
    Wins.Name = "Wins"
    local Tokens = Instance.new("IntValue",leaderstats)
    Tokens.Name = "Tokens"
    local Plr = Instance.new("Folder",game.ServerStorage:WaitForChild("PlayerInventory"))
    Plr.Name = plr.Name

    local WinsData
    local TokensData
    local InventoryData

    local success,errormessage = pcall(function()
        WinsData = WinsDataStore:GetAsync(plr.UserId.."-Wins")
        TokensData = TokensDataStore:GetAsync(plr.UserId.."-Tokens")
        InventoryData = InventoryDataStore:GetAsync(plr.UserId.."-Inventory")
    end)
    if success then
        Tokens.Value = TokensData
        print(TokensData)
        Wins.Value = WinsData
        print(WinsData)


        if InventoryData then
            print(InventoryData)
            print(tostring(InventoryData))
            for i,skin in pairs(http:JSONDecode(tostring(InventoryData))) do
                print(skin.Name..": hi")
                local playerInventory = game.ServerStorage:WaitForChild("PlayerInventory")
            end
        end
        print("Loaded data.")
    else
        warn("DataStore error: ",errormessage)
    end
end)

game.ReplicatedStorage.events.ServerTokenUI.OnServerEvent:Connect(function(plr)
    local tokens = plr.leaderstats.Tokens
    game.ReplicatedStorage.events.ClientTokenUI:FireClient(plr,tokens)
end)

local bindableEvent = Instance.new("BindableEvent")
game.Players.PlayerRemoving:Connect(function(plr)
    local success,errormessage = pcall(function()
        WinsDataStore:SetAsync(plr.UserId.."-Wins",plr.leaderstats.Wins.Value)
        TokensDataStore:SetAsync(plr.UserId.."-Tokens",plr.leaderstats.Tokens.Value)
        local skins = {}
        local inventory = game.ServerStorage.PlayerInventory[plr.Name]
        for _,skin in pairs(game.ServerStorage:WaitForChild("Skins"):GetChildren()) do
            if inventory:FindFirstChild(skin.Name) then
                table.insert(skins,skin)
                print("Added: "..skin.Name)
            end
            print(skin)
        end
        InventoryDataStore:SetAsync(plr.UserId.."-Inventory",http:JSONEncode(skins))

        if #game.Players:GetPlayers() <= 1 then
            bindableEvent:Fire()
        end
    end)
    if success then
        print("Saved data.")
    else
        warn("DataStore Error: ",errormessage)
    end
end)
game:BindToClose(function()
    bindableEvent.Event:Wait()
end)

1 answer

Log in to vote
0
Answered by 3 years ago

Hi! Sorry, I'm not the best at answering questions but I hope this helps!

Datastores can only store characters, not objects. If you want to store a list of objects, try using HTTPService so you can convert the table into a string with HTTPService:JsonEncode(table)

When you want to turn the string back into a table, you can then do HTTPService:JsonDecode(string).

Hope this helps!

0
when I loaded all of the data i got an error - JsonDecode is not a valid member of HttpService "HttpService" jorcorrs 76 — 3y
0
sorry meant HTTPService:JSONDecode! json in all caps! Try checking the documentation https://developer.roblox.com/en-us/api-reference/class/HttpService CocoDysphoria 67 — 3y
0
okay, thanks! I'll see if this works jorcorrs 76 — 3y
0
ServerScriptService.leaderstats:35: invalid argument #1 to 'pairs' (table expected, got string) jorcorrs 76 — 3y
View all comments (5 more)
0
I edited the script to show you what i did jorcorrs 76 — 3y
0
try to remove the tostring peter21340 41 — 3y
0
and also try to and see if it works: print(http:JSONDecode(tostring(InventoryData))) peter21340 41 — 3y
0
i did jorcorrs 76 — 3y
0
it prints an empty table, so i don't think it saved jorcorrs 76 — 3y
Ad

Answer this question