Why this line gives me error?

local profile = ProfileStore:LoadProfileAsync("Player_"..player.UserId)

attempt to index nil with ‘UserId’
full script:

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

local Template = require(ServerScriptService.PlayerData.Template)
local Manager = require(ServerScriptService.PlayerData.Manager)
local ProfileService = require(ServerScriptService.Libs.ProfileService)

local ProfileStore = ProfileService.GetProfileStore("Test", Template)-- Main

local KICK_MESSAGE = "Data loading issue, try again later"

local function CreateLeaderstats(player)
	local profile = Manager.Profiles[player]
	if not profile then return end
	
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
	
	local clicks = Instance.new("NumberValue", leaderstats)
	clicks.Name = "Clicks"
	clicks.Value = profile.Data.Clicks
	
	local diamonds = Instance.new("NumberValue", leaderstats)
	diamonds.Name = "Diamonds"
	diamonds.Value = profile.Data.Diamonds
end

local function LoadProfile(player)
	local profile = ProfileStore:LoadProfileAsync("Player_"..player.UserId)
	if not profile then
		player:Kick(KICK_MESSAGE)
		return
	end
	
	profile:AddUserId(player.UserId)
	profile:Reconcile()
	profile:ListenToRelease(function()
		Manager.Profiles[player] = nil
		player:Kick(KICK_MESSAGE)
	end)
	
	if player:IsDescendantOf(Players) == true then
		Manager.Profiles[player] = profile
		CreateLeaderstats(player)
	else
		profile:Release()
	end
end

for _, player in Players:GetPlayers() do
	task.spawn(LoadProfile, player)
end

Players.PlayerAdded:Connect(LoadProfile())
Players.PlayerRemoving:Connect(function(player)
	local profile = Manager.Profiles[player]
	if profile then
		profile:Release()
	end
end)

( yes that’s a guide from youtube)

Try this instead:
Players.PlayerAdded:Connect(LoadProfile)

3 Likes

Yes that work thanks you so mush for helping

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