Attempt to Index Nil with 'userId'

Hi!

I have a module that stores claimed table positions so that a player can generate a puzzle at that position. I’m struggling because I’m not sure why it says this error:

ReplicatedStorage.ClaimTableModule:16: attempt to index nil with ‘UserId’ - Server - ClaimTableModule:16

I’ve tried the available resources (such as the topics on dev forum) but it either gives me the error or breaks the whole code. I have a script that claims the table and it works.

local ClaimTableModule = {}
local claimedTables = {} -- Store claimed tables with player UserId

function ClaimTableModule.claimTable(player, tablePosition)
	local userId = tostring(player.UserId)

	if not claimedTables[userId] then
		claimedTables[userId] = tablePosition
		return true
	end

	return false
end

function ClaimTableModule.getPlayerClaimedTablePosition(player)
	local userId = tostring(game.Players.LocalPlayer.UserId)
	return claimedTables[userId]
end

return ClaimTableModule

Thank you :slight_smile:

You are trying to get the UserId of the local player, assuming that this is ran by the server you should change this line to

local userId = tostring(player.UserId)

Hmm, it says the same error for that line you gave me.

Are you passing the player object as a parameter when calling the function?

I think so? Sorry I’m new!! Here is my table claiming script which requires the claim table module.

-- ClaimTableScript

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local tableModel = script.Parent.Parent.Workspace.Assets.Seating.Table1
local part = tableModel:FindFirstChild("Picnic Table"):FindFirstChild("Part1")
local proximityPrompt = part:FindFirstChild("ClaimTablePrompt")
local tablePosition = part.Position

local ClaimTableModule = require(ReplicatedStorage:WaitForChild("ClaimTableModule"))

proximityPrompt.Triggered:Connect(function(player)
	local claimed = ClaimTableModule.claimTable(player, tablePosition)

	if claimed then
		print(player.Name .. " claimed the table at position: " .. tostring(tablePosition))
	else
		print(player.Name .. " has already claimed a table.")
	end

	proximityPrompt.Enabled = false
end)

I’m not seeing any usage of the ClaimTableModule.getPlayerClaimedTablePosition function, is that the one that is causing an error?

No, it was the first script that is causing the error. Should the table claiming script have ClaimTableModule.getPlayerClaimedTablePosition in it? Would that fix the error?

What script calls this function? Can you show it?

function ClaimTableModule.getPlayerClaimedTablePosition(player)
	local userId = tostring(game.Players.LocalPlayer.UserId)
	return claimedTables[userId]
end

This one does. It’s the puzzle generation script which is a regular script in ServerScriptService

-- PuzzleGenerationScript

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Workspace = game:GetService("Workspace")

local GeneratePuzzleEvent = ReplicatedStorage:WaitForChild("GeneratePuzzleEvent")
local ClaimTableModule = require(ReplicatedStorage:WaitForChild("ClaimTableModule"))

local player = game.Players.LocalPlayer

-- Function to generate the puzzle
function generatePuzzle()
	local claimedTablePosition = ClaimTableModule.getPlayerClaimedTablePosition(player)
	
	if claimedTablePosition then
		local jigsawPiecesFolder = ReplicatedStorage:WaitForChild("jigsaw_test1")
		local correctSnapPositionsFolder = ReplicatedStorage:WaitForChild("CorrectSnapPositions")

		local jigsawPieceModels = jigsawPiecesFolder:GetChildren()

		if #jigsawPieceModels > 0 then
			for _, jigsawPieceModel in ipairs(jigsawPieceModels) do
				local randomX = math.random(-smallerBoxSize.X, smallerBoxSize.X)
				local randomY = math.random(-smallerBoxSize.Y/2, smallerBoxSize.Y/2)
				local randomZ = math.random(-smallerBoxSize.Z/2, smallerBoxSize.Z/2)

				local randomPosition = claimedTablePosition + Vector3.new(randomX, randomY, randomZ)

				local randomJigsawPiece = jigsawPieceModel:Clone()

				if randomJigsawPiece:IsA("MeshPart") then
					randomJigsawPiece.CFrame = CFrame.new(randomPosition)
				else
					warn("Invalid object type:", randomJigsawPiece.ClassName)
				end

				randomJigsawPiece.Parent = Workspace
			end
		else
			warn("No jigsaw piece models found in ReplicatedStorage.")
		end
	else
		warn("Player does not have a claimed table.")
	end
end

-- Connect the generatePuzzle function to the RemoteEvent
GeneratePuzzleEvent.OnServerEvent:Connect(generatePuzzle)

And you changed it to what Floofy said like this?

function ClaimTableModule.getPlayerClaimedTablePosition(player)
	local userId = tostring(player.UserId)
	return claimedTables[userId]
end

Yup, same error.

can you give me the place file so i can test it

Sorry I fell asleep :pensive:.

When you call the OnServerEvent it should pass the player, so i think to fix this you just need to get rid of the
local player = game.Players.LocalPlayer
and add the player to the function
function generatePuzzle(player).

THANK YOU!!! This fixed it. I appreciate your help! :slight_smile: