Player is nil? Unsure how to fix

I’m working on a Datastore system for my game which saves 3 values; coins, if they own the teacher role, and if they own the ‘other’ role. When I call the function SaveData, it always returns with ‘attempt to index nil with data’, and it can’t print the name of the Player either.

Heres the function and when it’s called;

function M:SaveData(Player)	
	local Data = {
		Player.Data.Coins.Value,
		Player.Data.OwnsTeacher.Value,
		Player.Data.OwnsOther.Value,
	}
	
	local s,e = pcall(function()
		CoinsStore:SetAsync(Player.UserId, Data)
	end)
	
	if s then
		print("Updated Datastore for "..Player.Name)
	elseif e then
		print(e)
	end
end

Players.PlayerRemoving:Connect(function(player)
	M.SaveData(player)
end)

game:BindToClose(function() 
	for _, player in pairs(game.Players:GetChildren()) do
		M.SaveData(player)
	end
end)

There are other systems that use the SaveData function, but they return the same error when they’re used.

Change M.SaveData(player) to M:SaveData(player). Since you are using : instead of ., the first argument will be self.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.