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!
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
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
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.