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

How would I create a table to go on a value already in a table?

Asked by 4 years ago

So basically I'm making an item saver, it saves via the items name (Such as "Basic Sword") but part of my game will be weapon upgrades, enchanting and such.

The saving works so far but currently doesn't save the values within the sword.

How would one create a table connected to the value. Such as:

local saveTable = {SwordA = {Upgrade=12, Enchant="CritChance3", Effect="Circle5"}, SwordB = {Upgrade=5, Enchant="Sharp5", Effect="Spark3"}}

My script currently is as follows:

local dataStore = game:GetService("DataStoreService")
local itemSaves = dataStore:GetDataStore("playersItems")

local gameItemsFolder = game.ServerStorage.Items
local swordFolder = gameItemsFolder.Swords
local potionFolder = gameItemsFolder.Potions

local function saveItems(lostPlayer)
    local Key = lostPlayer.UserId .. "-Items"
    local SaveTable = {Swords={}, Potions={}}

    local starterGear = lostPlayer:WaitForChild("StarterGear")

    for _,item in pairs(starterGear:GetChildren()) do
        if item:FindFirstChild("weaponServer") then
            table.insert(SaveTable.Swords, item.Name)

        elseif item:FindFirstChild("potionServer") then
            table.insert(SaveTable.Potions, item.Name)
        end
    end
end

local function loadItems(newPlayer)
    local Key = newPlayer.UserId .. "-Items"
    local CanLoad = true
    local playerItems = itemSaves:GetAsync(Key)

    local Backpack = newPlayer:WaitForChild("Backpack")
    local starterGear = newPlayer:WaitForChild("StarterGear")

    if playerItems == nil then
        CanLoad = false
    end

    if CanLoad == false then
        return nil
    else
        for _,v in pairs(playerItems.Swords) do
            local foundItem = swordFolder:FindFirstChild(v)
            if foundItem then
                local cloneA = foundItem:Clone()
                local cloneB = foundItem:Clone()

                cloneA.Parent = Backpack
                cloneB.Parent = starterGear
            else
                warn("Item: '" .. v .. "' Not Found")
            end
        end

        for _,v in pairs(playerItems.Potions) do
            local foundItem = potionFolder:FindFirstChild(v)
            if foundItem then
                local cloneA = foundItem:Clone()
                local cloneB = foundItem:Clone()

                cloneA.Parent = Backpack
                cloneB.Parent = starterGear
            else
                warn("Item: '" .. v .. "' Not Found")
            end
        end
    end
end

game.Players.PlayerAdded:Connect(function(newPlayer)
    wait(2)
    if loadItems(newPlayer) == nil then
        local startItems = gameItemsFolder.starterItems

        for _,v in pairs(startItems:GetChildren()) do
            local Backpack = newPlayer:WaitForChild("Backpack")
            local starterGear = newPlayer:WaitForChild("StarterGear")
            local playerArmour = newPlayer:WaitForChild("playerArmour")
            local playerEnchants = newPlayer:WaitForChild("playerEnchants")

            local itemType = v:FindFirstChild("itemType")

            if itemType.Value == "Weapon" or itemType.Value == "Potion" then
                v:Clone().Parent = Backpack
                v:Clone().Parent = starterGear
            elseif itemType.Value == "Armour" then
                v:Clone().Parent = playerArmour
            elseif itemType.Value == "Enchant" then
                v:Clone().Parent = playerEnchants
            end
        end
    end
end)

game.Players.PlayerRemoving:Connect(function(lostPlayer)
    wait()
    saveItems()
end)

Answer this question