How would I create a table to go on a value already in a table?
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:
01 | local dataStore = game:GetService( "DataStoreService" ) |
02 | local itemSaves = dataStore:GetDataStore( "playersItems" ) |
04 | local gameItemsFolder = game.ServerStorage.Items |
05 | local swordFolder = gameItemsFolder.Swords |
06 | local potionFolder = gameItemsFolder.Potions |
08 | local function saveItems(lostPlayer) |
09 | local Key = lostPlayer.UserId .. "-Items" |
10 | local SaveTable = { Swords = { } , Potions = { } } |
12 | local starterGear = lostPlayer:WaitForChild( "StarterGear" ) |
14 | for _,item in pairs (starterGear:GetChildren()) do |
15 | if item:FindFirstChild( "weaponServer" ) then |
16 | table.insert(SaveTable.Swords, item.Name) |
18 | elseif item:FindFirstChild( "potionServer" ) then |
19 | table.insert(SaveTable.Potions, item.Name) |
24 | local function loadItems(newPlayer) |
25 | local Key = newPlayer.UserId .. "-Items" |
27 | local playerItems = itemSaves:GetAsync(Key) |
29 | local Backpack = newPlayer:WaitForChild( "Backpack" ) |
30 | local starterGear = newPlayer:WaitForChild( "StarterGear" ) |
32 | if playerItems = = nil then |
36 | if CanLoad = = false then |
39 | for _,v in pairs (playerItems.Swords) do |
40 | local foundItem = swordFolder:FindFirstChild(v) |
42 | local cloneA = foundItem:Clone() |
43 | local cloneB = foundItem:Clone() |
45 | cloneA.Parent = Backpack |
46 | cloneB.Parent = starterGear |
48 | warn( "Item: '" .. v .. "' Not Found" ) |
52 | for _,v in pairs (playerItems.Potions) do |
53 | local foundItem = potionFolder:FindFirstChild(v) |
55 | local cloneA = foundItem:Clone() |
56 | local cloneB = foundItem:Clone() |
58 | cloneA.Parent = Backpack |
59 | cloneB.Parent = starterGear |
61 | warn( "Item: '" .. v .. "' Not Found" ) |
67 | game.Players.PlayerAdded:Connect( function (newPlayer) |
69 | if loadItems(newPlayer) = = nil then |
70 | local startItems = gameItemsFolder.starterItems |
72 | for _,v in pairs (startItems:GetChildren()) do |
73 | local Backpack = newPlayer:WaitForChild( "Backpack" ) |
74 | local starterGear = newPlayer:WaitForChild( "StarterGear" ) |
75 | local playerArmour = newPlayer:WaitForChild( "playerArmour" ) |
76 | local playerEnchants = newPlayer:WaitForChild( "playerEnchants" ) |
78 | local itemType = v:FindFirstChild( "itemType" ) |
80 | if itemType.Value = = "Weapon" or itemType.Value = = "Potion" then |
81 | v:Clone().Parent = Backpack |
82 | v:Clone().Parent = starterGear |
83 | elseif itemType.Value = = "Armour" then |
84 | v:Clone().Parent = playerArmour |
85 | elseif itemType.Value = = "Enchant" then |
86 | v:Clone().Parent = playerEnchants |
92 | game.Players.PlayerRemoving:Connect( function (lostPlayer) |