Sub-Table data doesn't load, invalid arguement #1 to 'ipairs'?
Ok so, I'm making a mining simulator, and I want to make a tool DataStore.
For some reason, the data won't load properly.
Script I used:
001 | local DataStoreService = game:GetService( "DataStoreService" ) |
002 | local DS = DataStoreService:GetDataStore( "MinerzData" ) |
004 | local function loadData(player) |
005 | local key = player.UserId .. "-minerzData" |
008 | local success,errorMessage = pcall ( function () |
009 | data = DS:GetAsync(key) |
013 | print ( 'MinerzData | Successfully loaded ' .. player.Name .. "'s data!" ) |
015 | elseif errorMessage then |
016 | warn( 'MinerzData | ' .. errorMessage) |
021 | local function saveData(player) |
022 | local key = player.UserId .. "-minerzData" |
024 | player.Items.Wood.Value, |
025 | player.Items.Stone.Value, |
026 | player.leaderstats.Cash.Value, |
032 | for i, pickaxe in pairs (player.Backpack:GetChildren()) do |
033 | if pickaxe:IsA( "Tool" ) then |
034 | table.insert(data.pickaxes, pickaxe.Name) |
040 | local success,errorMessage = pcall ( function () |
041 | DS:SetAsync(key, data) |
045 | print ( 'MinerzData | Successfully saved ' .. player.Name .. "'s data!" ) |
047 | elseif errorMessage then |
048 | warn( 'MinerzData | ' .. errorMessage) |
053 | game.Players.PlayerAdded:Connect( function (player) |
054 | local playerItems = Instance.new( "Folder" , player) |
055 | playerItems.Name = "Items" |
057 | local leaderstats = Instance.new( "Folder" , player) |
058 | leaderstats.Name = "leaderstats" |
060 | local cash = Instance.new( "IntValue" , leaderstats) |
064 | for i, item in pairs (game:GetService( "ServerStorage" ):WaitForChild( "Items" ):GetChildren()) do |
065 | local newVal = Instance.new( "IntValue" , playerItems) |
066 | newVal.Name = item.Name |
070 | local data = loadData(player) |
073 | warn(player.Name .. " must be a new player; data not found!" ) |
075 | playerItems:WaitForChild( "Wood" ).Value = data [ 1 ] |
076 | playerItems:WaitForChild( "Stone" ).Value = data [ 2 ] |
078 | for i, pickaxe in ipairs (data.pickaxes) do |
079 | local exists = game:GetService( "ReplicatedStorage" ):WaitForChild( "Pickaxes" ):FindFirstChild(pickaxe) |
081 | local newPick = exists:Clone() |
082 | local newPick 2 = exists:Clone() |
083 | newPick.Parent = player:WaitForChild( "Backpack" ) |
084 | newPick 2. Parent = player:WaitForChild( "StarterGear" ) |
089 | player.CharacterRemoving:Connect( function (char) |
090 | char:WaitForChild( "Humanoid" ):UnequipTools() |
094 | game.Players.PlayerRemoving:Connect( function (player) |
100 | game:BindToClose( function () |
101 | for i, player in pairs (game.Players:GetPlayers()) do |
On line 78, it errors with:
1 | ServerScriptService.InventoryHandler: 79 : invalid argument # 1 to 'ipairs' (table expected, got nil ) |
On line 23 I define the "data" table in the saveData() function. I also define "pickaxes" as a sub-table. (line 27)
For some reason, when I join the game, it says table expected, got nil.
I don't know why this happens, and when I do a print(data.pickaxes), it just prints "nil."
If someone could help, it would mean a lot to me!