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

Inserting IntValues into a table?

Asked by
dirk2999 103
6 years ago

Here's my script,

local carts = {
    ["red"] = 1,
    ["white"] = 2,
    ["blue"] = 3
}

local DSService = game:GetService('DataStoreService')
local Save = DSService:GetDataStore('Carts1')

game.Players.PlayerAdded:connect(function(player)
    pcall(function()
        player:WaitForChild("PlayerGui")
        local saveData = Save:GetAsync(player.userId)
        Save:SetAsync(player.userId,carts)
        print("Done!")
        wait(4)
        if saveData then
            for i,v in pairs(saveData) do
                Int = Instance.new("IntValue")
                Int.Name = i
                Int.Value = v
                Int.Parent = game.Workspace.Test
            end

        end 
    end)
end)

So basically what I want to do is when a player joins it gets the table of values and inserts them into an IntValue in the player (Right now its going to workspace for testing purposes, will be in the player). My question is if the carts table was empty how would I insert the IntValues into the table to save? I tried,

local carts = {

}

for i,v in pairs(game.Workspace.Test:GetChildren()) do
    table.insert(carts,i,v)
end

It returned that the game cannot save array values, I do not know how to change that.

1 answer

Log in to vote
0
Answered by 6 years ago

Try:

for _, v in pairs(workspace.Test:GetChildren()) do
    carts[v.Name] = v.Value
end
Ad

Answer this question