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
Consider fixing your formatting. Get them all on their appropriate indentations. ROBLOX Studio usually automatically does most of this for you.
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.