Script keeps erroring, anyone able to help?

  1. What do you want to achieve?
    I am wanting a script which detects whether a certain person owns a gamepass, and if they do, it destroys the part they touch. (Basically for premium/gamepass limited areas)

  2. What is the issue?
    Everytime you make contact with the part, it throws the error of “Workspace.PremiumLoungeDoor.Script:4: attempt to index nil with ‘UserId’” & spams it continuously.

  3. What solutions have you tried so far?
    I’ve checked here and around the web and couldn’t find much of a solution. I’m not a scripter by any means, just revamping a game, any help would be very appreciated!

That’s because .Touched event detects any other parts that isn’t player. You need to check if the part touching is a character of a player before you do the UserOwnsGamePassAsync function.

Example: You have a baseplate part that touches it, that’s why it keeps firing. And assuming that part is a baseplate, the parent would be Workspace. You can’t do UserOwnsGamePassAsync on a Workspace.

Think of it like this:
MarketPlaceService:UserOwnsGamePassAsync(Workspace, yourIdHere) which would return nil (nothing) of course.

Hello! That’s because the player is nil.

Touched function returns the part that touched it, It’s continuously erroring like that because It’s touching parts that are not the player.

Make sure to check if the player exists.

if player then
--continue
end

I’m not a scripter, I understand somewhat of what you mean, what would I need to add/remove to the script for it to only detect the player?

You can just check if the player variable is not nil, just as I said.

After you set the player variable:

if player then
--Continue script
end

Keep in mind that destroying the door on the server will destroy it for everyone, so anyone can enter your lounge if someone with the gamepass enters (unless that’s what you’re going for)

Also please copy and paste your code instead of screenshotting it, as outlined in category guidelines.

This script can be thrown in StarterPlayerScripts, it will destroy the part when the player joins (unaffecting other clients)

local MarketplaceService = game:GetService('MarketplaceService')
local Player = game:GetService('Players').LocalPlayer
local passId = 0 -- The gamepass ID

if MarketplaceService:UserOwnsGamePassAsync(Player.UserId,passId) then
    print('Player owns pass')
    workspace.Door:Destroy()
end

Thank you! Seems to work great, didn’t think about it destroying for everyone, thank you again.