Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why does my table read as nil when placed in a for i, v in pairs segment?

local Players = game:GetService("Players")
local DataStore = game:GetService("DataStoreService")
local myDataStore = DataStore:GetDataStore("myDataStore")

local function addtoplayer(Player,v,i)
	local value = game.Players.player.Inventory.otherValues
	if i == 1 then value.formMastery.Value = v
	elseif i == 2 then value.powerMastery.Value = v
	elseif i == 3 then value.powerAlignment.Value = v
	elseif i == 4 then value.Credits.Value = v
					
	end
end
Players.PlayerAdded:Connect(function(player)
	local playerName = player.Name 
	local Player = player
	local newInventory = game.ServerStorage.Inventory:Clone()
	newInventory.Parent  = game.Players[playerName]
		local id = "player_"..player.UserId
		local data ={}
		local s, e = pcall(function()
			data = myDataStore:GetAsync(id)
		end)
			if s == true then
				local value = game.Players[playerName].Inventory.otherValues
				for i, v in pairs(data) do
					 local Player = player
					if i == 1 or 2 or 3 or 4 then
						addtoplayer(Player,v,i)
					elseif i == 5 then
					end
				end	
		elseif s == false then
	local value = math.random(1, 3)
	local playerdefined = game.Players[player.Name]
	playerdefined.Inventory.otherValues.powerMastery.Value = value
	local value = math.random(1,7)
	playerdefined.Inventory.otherValues.formMastery.Value = value
	playerdefined.Inventory.otherValues.powerAlignment.Value = 0
	playerdefined.Inventory.otherValues.Credits.Value = 100
	end
end)

Players.PlayerRemoving:Connect(function(player)
local id = "player_"..player.UserId
local value = game.Players.player.Inventory.otherValues
local data ={} 
table.insert(data,1,value.formMastery.Value)
table.insert(data,2,value.powerMastery.Value)
table.insert(data,3, value.powerAlignment.Value)
table.insert(data,4,value.Credits.Value)
local itemTable ={}
table.insert(data,5,itemTable)
	myDataStore:SetAsync(id, data)
	print("sucses")
end)

The error is one line #26 "bad argument #1 (table expected, got nil) The script is a pretty basic one just saving data and calling it back, the problem I have is when on line 26, it claims that data is nil and it expected a table, however according to what I understand data is a table?

5 comments

0

If the datastore is empty then it will return nil, if you didn't assign any value to the player's data then it will just set the variable data to nil

0

Doesn't the if s == true check this?

0

There's nothing in 'data'

0

"Doesn't the if s == true check this?"

0

No, it will not throw an error if the data is empty, it will only return nil

1 answer

The error you are getting is that data is nil. This is likely happening because the DataStore is empty. To avoid this error, make sure to check that it is not nil before proceeding. For example:

if (data ~= nil) then
	-- data exists, do stuff
end

This discussion is preserved as it was. New answers, comments, and votes are closed.