Attempt to index nil with 'UserId'

Hey there, I wanted to make a GUI that changes the part’s color if you have a gamepass. But the script doesn’t work. It says “attempt to index nil with ‘UserId’”. I don’t understand why this script doesn’t work, since if I put my own UserId rather than “player.UserId” it works completely fine.

I tried changing the script based on Devforum posts and Youtube tutorials but none of it worked.

If you could help me out, I would really appreciate it.

Here’s is the script:
Screenshot_73

local player = game.Players.LocalPlayer
local gamepassId = 92766320
script.Parent.Frame.DarkRed.MouseButton1Click:Connect(function()
	if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(player.UserId,gamepassId) == true then
		game.Workspace.Color.Part1.BrickColor = BrickColor.new("Crimson")
	else
		print("failed")
	end
end)

This needs to be a local script if you are using LocalPlayer.

Try printing out player’s classname like so

print(player.ClassName)

and see what it prints.

Thank you so much! I haven’t used LocalScripts yet.