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

GetAsync() doesnt return table (?)

Asked by 4 years ago

I have a piece of code:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DataStoreService = game:GetService("DataStoreService")
local wordsStore = DataStoreService:GetDataStore("words")
wordsStore:SetAsync("words", {"book","bone","happy"})
local function insert(plr, word)
    local function update(oldTable)
        return table.insert(oldTable, 1 , word)
    end
    wordsStore:UpdateAsync("words", update)
end
ReplicatedStorage.Insert.OnServerEvent:Connect(insert)
while wait(5) do
    for _, v in pairs(table.insert(wordsStore:GetAsync("words"),1,"horse")) do
        print(v)
    end
end

In the while loop i've set it to:

while wait(5) do
    for _, v in pairs(wordsStore:GetAsync("words")) do
        print(v)
    end
end

And it works, it prints every item in the table but when set it up like this

while wait(5) do
    for _, v in pairs(table.insert(wordsStore:GetAsync("words"),1,"horse")) do
        print(v)
    end
end

It says: ServerScriptService.Script:13: missing argument #1 (table expected) But in the example without the table.insert it recognized it as a table (because the in pairs thingy works) but in table.insert it doesnt and that's why the code to update the table in the beggining doesn't work. How do i fix that?

1 answer

Log in to vote
0
Answered by 4 years ago

You have to store the table like this:

local dataStoreService = game:GetService("DataStoreService")
local tableData = dataStoreService:GetDataStore("TableTest")

local function getData()
    local gotTable = {}
    local success, failure = pcall(function()
        gotTable = tableData:GetAsync("RandomKey")
    end)
end

game.Players.PlayerAdded:Connect(function()
    local dataTable = getData()

    for _,v in pairs(dataTable) do
        print(_, v)
    end
end)

game.Players.PlayerRemoving:Connect(function()
    tableData:SetAsync("RandomKey", {
        ["Hello"] = 5,
        ["World"] = 10
    })
end)

Maybe not the best example but it should do.

Ad

Answer this question