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

How do I deal with tables in a datastore?

Asked by 6 years ago

Hi guys, I'm struggling with this datastore stuff... Idk why I can't understand it :P

The following code pretty much sums up my problem:

local data_store_service = game:GetService("DataStoreService")

local function player_added(player)
    local scope = "player_" .. player.UserId
    local player_data = data_store_service:GetDataStore("player_data",scope)
        local experience = player_data:SetAsync("experience",0)     
        local resources = player_data:SetAsync("resources",{})
            local wood = --how do I get a number value with the key "wood" inside of the resources table?

    local bank_data = data_store_service:GetDataStore("bank_data",scope)
end

game:GetService("Players").PlayerAdded:connect(player_added)

How do I add values into the table? If I make the line read: resources["wood"] = 0, I get an error saying that resources is a nil value...

I would usually have all of this stuff stored as values in a folder in serverstorage so it wouldn't be a problem but i'm trying to move away from that...

Thanks for your help guys, I think it's a really easy question tbh but I can't figure it out haha

P.S. If anyone know of a good place to learn about how to setup large datastores (all I can find is could you please leave a link? ty :)

1 answer

Log in to vote
0
Answered by 6 years ago

The reason why it says that is because SetAsync doesn't return anything. (http://wiki.roblox.com/index.php?title=API:Class/GlobalDataStore/SetAsync)

You'll need to use GetAsync and then do it like you were thinking.

local resources = player_data:GetAsync("resources")
resources['wood'] = 0
player_data:SetAsync("resources",resources)
0
oh right thanks :) AlexTheCreator 461 — 6y
Ad

Answer this question