Error with DataStore "attempt to index nil with UserId"

Hello! I am attempting to try and make a card collecting game, but whenever I set up the DataStore using ProfileStore, I get an error along the lines of this:

ServerScriptService.Library.Server.Datastore:39: attempt to index nil with ‘UserId’ - Server - Datastore:39
Stack Begin - Studio
Script ‘ServerScriptService.Library.Server.Datastore’, Line 39 - function InitializePlayerData - Studio - Datastore:39

I tried re-doing the scripts, and trying to change the name of the session, but nothing worked.

	Profiles = {}
}

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local ServerScriptService = game:GetService("ServerScriptService")

function Datastore:Initiate()
	
end

function Datastore:Start()
	
end

local function GetDatastoreName()
	return RunService:IsStudio() and "studio-testing" or "production"
end

local ProfileStore = require(ServerScriptService:WaitForChild("ProfileStore"))
local PlayerData = require(game:GetService("ReplicatedStorage").Modals.Data:WaitForChild("PlayerData"))
local PlayerStore = ProfileStore.New(GetDatastoreName(), PlayerData)

function Datastore:InitializePlayerStats(player: Player, profile: typeof(PlayerStore:StartSessionAsync()))
	local leaderstats = Instance.new("Folder", player)
	leaderstats.name = "leaderstats"
	
	local Cardbux = Instance.new("NumberValue", leaderstats)
	Cardbux.Name = "💵 Card Bux 💵"
	Cardbux.Value = profile.Data.currency.cardbux
	
	local IndexedCards = Instance.new("NumberValue", leaderstats)
	IndexedCards.Name = "📬 Indexed Cards 📬"
	IndexedCards.Value = #profile.Data.inventory.cards
end

function Datastore:InitializePlayerData(player: Player)
	local profile = PlayerStore:StartSessionAsync("player_"..player.UserId, {
		Cancel = function()
			return player.Parent ~= Players
		end,
	})

	if profile then
		profile:AddUserId(player.UserId)
		profile:Reconcile()

		profile.OnSessionEnd:Connect(function()
			player:Kick("An error occurred while loading / creating your data. Please rejoin. If this becomes a recurring issue, please reach out on Discord.")
		end)

		if player.Parent == Players then
			Datastore.Profiles[player.UserId] = profile
		else
			profile:EndSession()
		end
	else
		player:Kick("An error occurred while loading / creating your data. Please rejoin. If this becomes a recurring issue, please reach out on Discord.")
	end
end


function Datastore:DestroyPlayerData(player: Player)
	local profile = Datastore.Profiles[player.UserId]
	if profile then
		profile:EndSession()
		Datastore.Profiles[player.UserId] = nil
	end
end

return Datastore

If anyone could help, it would be greatly appreciated!

Hi liam! I can try to help.

What your error means is that basically at line 39 your script is doing “nil.UserId.”

Unless there’s more to your script above, line 39 seems to be

local profile = PlayerStore:StartSessionAsync("player_"..player.UserId, {

So, All I can think of is that when you are calling your function InitializePlayerData, you are passing a nil value to it as the player.

If knowing this doesn’t fix it right away, can you send more of your code including the usage of the functions?

That is an odd error for this and it looks like some of this script is missing. You may be jumping the gun here and the player isn’t even loaded yet.. How else could that be nil ??

If he passes the player to the Database module through PlayerAdded, the Player instance will NEVER be nil. The only way it can be nil is if he passes no arguments to the Datastore:InitializePlayerData

But

yes some part of the scripts is missing and, depending on how many, the error could not even be in the snippet we currently have

Just speculating… mad I don’t see a way to help here. Datastore problems are the worst problems.

1 Like

This is my current script, and I can confirm the error points to the player.UserId.

local Datastore = {
	Profiles = {}
}

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local ServerScriptService = game:GetService("ServerScriptService")

function Datastore:Initiate()
	
end

function Datastore:Start()
	
end

local function GetDatastoreName()
	return RunService:IsStudio() and "studio-testing" or "production"
end

local ProfileStore = require(ServerScriptService:WaitForChild("ProfileStore"))
local PlayerData = require(game:GetService("ReplicatedStorage").Modals.Data:WaitForChild("PlayerData"))
local PlayerStore = ProfileStore.New(GetDatastoreName(), PlayerData)

function Datastore:InitializePlayerStats(player: Player, profile: typeof(PlayerStore:StartSessionAsync()))
	local leaderstats = Instance.new("Folder", player)
	leaderstats.name = "leaderstats"
	
	local Cardbux = Instance.new("NumberValue", leaderstats)
	Cardbux.Name = "💵 Card Bux 💵"
	Cardbux.Value = profile.Data.currency.cardbux
	
	local IndexedCards = Instance.new("NumberValue", leaderstats)
	IndexedCards.Name = "📬 Indexed Cards 📬"
	IndexedCards.Value = #profile.Data.inventory.cards
end

function Datastore:InitializePlayerData(player: Player)
	local profile = PlayerStore:StartSessionAsync("player_"..player.UserId, {
		Cancel = function()
			return player.Parent ~= Players
		end,
	})

	if profile then
		profile:AddUserId(player.UserId)
		profile:Reconcile()

		profile.OnSessionEnd:Connect(function()
			player:Kick("An error occurred while loading / creating your data. Please rejoin. If this becomes a recurring issue, please reach out on Discord.")
		end)

		if player.Parent == Players then
			Datastore.Profiles[player.UserId] = profile
		else
			profile:EndSession()
		end
	else
		player:Kick("An error occurred while loading / creating your data. Please rejoin. If this becomes a recurring issue, please reach out on Discord.")
	end
end


function Datastore:DestroyPlayerData(player: Player)
	local profile = Datastore.Profiles[player.UserId]
	if profile then
		profile:EndSession()
		Datastore.Profiles[player.UserId] = nil
	end
end

return Datastore

This is the server script that calls it.

local Library = require(game.ReplicatedStorage.Library:WaitForChild("Library"))

game.Players.PlayerAdded:Connect(function(player)
	Library.Server.Datastore.InitializePlayerData(player)
	Library.Server.Datastore.InitializePlayerStats(player)
end)

game.Players.PlayerRemoving:Connect(function(player)
	Library.Server.Datastore.DestroyPlayerData(player)
end)

Ok then. I would suggest adding “print(player, typeof(player))” statements at the beginning of your functions inside the first script, and then we can go from there.

I’ve been reviewing your code, and the error occurs because the function that handles the player data is defined with a colon ( : ), but in your script, you’re calling it with a dot ( . ). This causes the player parameter to never arrive correctly and ends up being nil.

You can fix this in two ways: either change the function definition to use a dot ( . ):

function Datastore.InitializePlayerData(player)

or you change the call to use ( : ):

Datastore:InitializePlayerData(player)

Decide which of the two you want to keep so they both match.
Hopefully this will solve your problem.

1 Like

I checked that and yes, @coolgueo is correct! I should have evaluated your code more carefully lol. But yeah try that. It will probably fix it.