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

Argument null or missing in my table?

Hey guys, upon joining my game I get an 'Argument 2 missing or nil" for Line 29 on this script

-- Setup table that we will return to scripts that require the ModuleScript.
local PlayerStatManager = {}
 
local DataStoreService = game:GetService('DataStoreService')
local playerData = DataStoreService:GetDataStore('PlayerData')

-- Table to hold all of the player information for the current session.
local sessionData = {}
 
-- Function the other scripts in our game can call to change a player's stats. This
-- function is stored in the returned table so external scripts can use it.
function PlayerStatManager:ChangeStat(player, statName, changeValue)
	sessionData[player][statName] = sessionData[player][statName] + changeValue
end
 
local function getPlayerData(player)
	return playerData:GetAsync(player.UserId)
end

local function savePlayerData(player)	
playerData:SetAsync(player.UserId, sessionData[player])
end

-- Function to add player to the sessionData table.
local function setupPlayerData(player)
	local data = playerData:GetAsync(player.UserId)
	if not data then
		data = {hp = 0, s = 0}
		playerData:SetAsync(player.UserId, sessionData[player])
	end
	sessionData[player] = data
end
 
-- Bind setupPlayerData to PlayerAdded to call it when player joins.
game.Players.PlayerAdded:connect(setupPlayerData)


game.Players.PlayerRemoving:connect(function(player)
	savePlayerData(player)
	-- We don't need to keep track of the data anymore
	sessionData[player] = nil
	local DataStore = game:GetService("DataStoreService")
	local ds1 = DataStore:GetDataStore("coincurrency")
	print("Saving currency to data store...")	
	ds1:SetAsync(player.UserId, player.leaderstats.Coins.Value)	
	print("Currency Saved")
end)

game.Players.PlayerAdded:connect(function(player)	
	local data = playerData:GetAsync(player.UserId)
		local function getPlayerData(player)
		if data.hp == 1 then
		print("hi)")
		end
	return playerData:GetAsync(player.UserId)
end
end)

-- Return the PlayerStatManager table to external scripts can access it.
return PlayerStatManager


Anyone have any idea why it is giving me such an error?

2 comments

0

Consider fixing your formatting. Get them all on their appropriate indentations. ROBLOX Studio usually automatically does most of this for you.

0

Move Line 31 to under Line 26. Problem solved. You are setting the value after you index it, not before, meaning it does not exist when you are running the function.

1 answer

sessionData[player] does not exist at the time, so it evaluates to nil. Since nil is considered as being "no value" and SetAsync expects you to give two arguments, it errors. The solution would be to make sure sessionData[player] exists.

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