Attempt to index nil with 'UserId'

im making a Saving script and i keep getting the same error all the time and i dont know why, please help me Developers

Script

local DataSystem = {}

-- Services
local Players = game:GetService("Players")

-- Modules
local ProfileService = require(script.ProfileService)
local UserData = require(script.UserData)

-- Variables
local DataVersion = 01
local DataStoreKey = "playerData-"..tostring(DataVersion)
local GameProfileStore = ProfileService.GetProfileStore(DataStoreKey, UserData)
local Profiles = {}

function DataSystem:GetData(Player)
	local Profile = Profiles[Player]
	if Profile == nil then Player:Kick("Data Wasn't Found, Please Rejoin!") return end
	
	return Profiles[Player].Data
end

function DataSystem:LoadProfile(Player)
	local Key = "Player".. tostring(Player.UserId)
	local Profile = GameProfileStore:LoadProfileAsync(Key, "ForceLoad")
	
	print(Profile)
	
	if Profile == nil then Player:Kick("Data Wasn't Found, Please Rejoin!") return end
	
	Profile:Recocile()
	Profile:ListenToRelease(function()
		Profiles[Player] = nil
		Player:Kick(ProfileService.ReleaseKickMessage)
	end)
	
	if Player:IsDecendantOf(Players) then
		Profiles[Player] = Profile
		
		LoadData(Player)
	else
		Profile:Release()
	end
end

function DataSystem:RemoveProfile(Player)
	local Profile = Profiles[Player]
	if Profile == nil then warn(Player.Name.."'s Profile Is Nil") return end
	
	Profile:Release()
end

function LoadData(Player)
	local Profile = Profiles[Player]
	if Profile == nil then warn(Player.Name.."'s Profile Is Nil") return end
	
	local leaderstats = Instance.new("Folder", Player)
	leaderstats.Name = "leaderstats"
	
	local Data = Profile.Data
	
	local cash = Instance.new("IntValue", leaderstats)
	cash.Name = "Cash"
	cash.Value = Data.Cash
end
return DataSystem

Error:

I don’t think you need “tostring” I could be wrong.

i am still getting the error so i think its not that

it says you cant index nil with UserId, meaning that the “Player” parameter is nil, check if your passing the player parameter when your calling the function

because you haven’t put in the actual player instance using the DataSystem:LoadProfile
must be assuming you put the name of the player instead of the player instance

game.Players.Player1.Name is a string and
game.Players.Player1 is an instance

the string does not have UserId but the instance does

(never saw the function call to use that function)

Make sure that when you’re firing :LoadProfile() that you’re actually passing the player, pretty sure player is equal to nil.

You can also add a print to line 24 before local Key

i think i have connected it, let me send you the server script,

local Players = game:GetService("Players")
local DataSystem = require(script.Parent.ServerScripts.DataSystem)

function PlayerAdded(player)
	DataSystem:LoadProfile(player)
	
	local function CharacterAdded(character)
		print(character)
	end
	
	CharacterAdded(player.Character or player.CharacterAdded.Wait())
	player.CharacterAdded:Connect(CharacterAdded)
end

function PlayerRemoving(player)
	DataSystem:RemoveProfile(player)
end

Players.PlayerAdded:Connect(PlayerAdded())

for _, player in pairs(Players:GetPlayers()) do
	task.spawn(function()
		PlayerAdded(player)
	end)
end

Players.PlayerRemoving:Connect(PlayerRemoving)
Players.PlayerAdded:Connect(PlayerAdded())

to:

Players.PlayerAdded:Connect(PlayerAdded)
2 Likes

Oh! I see the issue, change line:

Players.PlayerAdded:Connect(PlayerAdded())

to

Players.PlayerAdded:Connect(PlayerAdded)

< Honestly not entirely sure if this is the issue, but I think that it would clear out anything passing

it did work, ty but im getting another one now

ServerScriptService.ServerScripts.DataSystem:32: attempt to call missing method ‘Recocile’ of table

You had a typo, it’s Profile:Reconcile()

1 Like

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