Attempt to index 'nil' with "player"

Hello everyone, today while I was working on my new customization system, I ran into a bug that I didn’t think that I would encounter, as I have never had trouble with it before.

My code should duplicate the customization room and set its location to a random point, then teleport the player inside of it. However, this does not happen, and I instead get an error.

The customization room will spawn at a random point as intended, but the player’s CFrame will not be changed to that of the teleport point.

Here is the code in my local script:

local customizeRoom = game:GetService("ReplicatedStorage").CustomizeRoom

local function spawnCustomizeRoom(player)
	local clonedCustomizeRoom = customizeRoom:Clone()
	clonedCustomizeRoom:SetPrimaryPartCFrame(CFrame.new(math.random(0, 1000), 0, math.random(0, 1000)))
	clonedCustomizeRoom.Parent = game:GetService("Workspace")
	player.LocalPlayer.Character:SetPrimaryPartCFrame(clonedCustomizeRoom.PlayerTeleportPart.CFrame)
end

script.Parent.InputBegan:Connect(function(input, player) -- When a player clicks a button
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		spawnCustomizeRoom(player)
	end
end)

you are trying to get LocalPlayer of a player that you already have defined, and you don’t need to pass the player to the function. try this and let me know if it works.

local player = game:GetService('Players').LocalPlayer
local customizeRoom = game:GetService("ReplicatedStorage").CustomizeRoom

local function spawnCustomizeRoom()
	local clonedCustomizeRoom = customizeRoom:Clone()
	clonedCustomizeRoom:SetPrimaryPartCFrame(CFrame.new(math.random(0, 1000), 0, math.random(0, 1000)))
	clonedCustomizeRoom.Parent = workspace
	player.Character:SetPrimaryPartCFrame(clonedCustomizeRoom.PlayerTeleportPart.CFrame)
end

script.Parent.MouseButton1Click:Connect(spawnCustomizeRoom)

Worked, marked as solution, thank you!