Need help with fixing this error

So i keep getting a error saying “Door is not found in workspace.Gamepass” and it is, I tried many ways to fix this using findfirstchild but that did nothing.

Script:

local Door = game.Workspace:FindFirstChild("GamepassDoor"):FindFirstChild("Door") -- Location of Door
local DoorGamepass = 93221008 -- Put Your Gamepass Id
local Market = game:GetService("MarketplaceService")

if Door then -- Check if the Door exists before setting up the Touched event
	Door.Touched:Connect(function(hit)
		local Hum = hit.Parent:FindFirstChild("Humanoid")
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if Hum then
			if Market:UserOwnsGamepassAsync(player.UserId, DoorGamepass) then
				Door.CanCollide = false 
			else
				Door.CanCollide = true
			end
		end
	end)
else
	warn("Door not found in Workspace.GamepassDoor")
end
5 Likes

Did you try using :WaitForChild rather than :FindFirstChild?

1 Like

Either you wrote the name of the gamepass folder wrong or door, or the gamepass has more than 2 doors with the same name.

Try this script (idk if the gamepass folder is written right)

local DoorGamepass = 93221008 -- Put Your Gamepass Id
local Market = game:GetService("MarketplaceService")

local GamepassFolder = game.Workspace:FindFirstChild("Gamepass")

if GamepassFolder then
	local Door = GamepassFolder:FindFirstChild("Door")
	if Door then -- Check if the Door exists before setting up the Touched event
		Door.Touched:Connect(function(hit)
			local Hum = hit.Parent:FindFirstChild("Humanoid")
			local player = game.Players:GetPlayerFromCharacter(hit.Parent)
			if Hum then
				if Market:UserOwnsGamepassAsync(player.UserId, DoorGamepass) then
					Door.CanCollide = false 
				else
					Door.CanCollide = true
				end
			end
		end)
	else
		warn("Door not found in Workspace.GamepassDoor")
	end
else
	warn("gamepass folder not found")
end

Try printing workspace:GetChildren() and see if it’s even there. If it is, then copy it and try again

lmk what it prints so I can try and help out with debugging it

2 Likes

From my experience, and this could be wrong, things are loaded into the workspace in order from least parents to most, and scripts sometimes start running before something is loaded.

So like @Whincify said, try :WaitForChild() instead.

No not yet im going to do that in my script.

There is a lot of mixed-up code and redundant checks here!

local PassDoor = game.Workspace:FindFirstChild("GamepassDoor") -- NEVER use nested function calls, EVER!!
local Door = PassDoor and PassDoor:FindFirstChild("Door") -- Location of Door, will return nil if PassDoor is nil
if not Door then return end -- Why check this later? if it doesn't exist now then anything below it is going to break, so return now!

local DoorGamepass = 93221008 -- Put Your Gamepass Id
local Market = game:GetService("MarketplaceService");

-- No need to check, we wouldn't be here if Door didn't exist
--if Door then -- Check if the Door exists before setting up the Touched event
Door.Touched:Connect(function(hit)

	local Hum = hit.Parent:FindFirstChild("Humanoid") -- hit.Parent == Player (according to this find logic)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent) -- now we use Player but not as guaranteed (as above) but through the PlayersService? Humanoid is a descendant of character which is a descendant of Player
	if Hum then -- redundant if player is nil!!
		if Market:UserOwnsGamepassAsync(player.UserId, DoorGamepass) then --Hum could be valid but we made no check on player here?
			Door.CanCollide = false 
		else
			Door.CanCollide = true
		end
	end

end)
1 Like

nope i cant go though it and i own the gamepass and theres no errors in the script, see thats the problem it wont work.

1 Like

Roblox is kinda strange, I keep getting annoyed with the fact that you have to do workspace:WaitForChild("Door") to even have it know that such instance is in workspace.

However, I’ll help you out, hope this works…

local Door = game.Workspace:WaitForChild("GamepassDoor"):WaitForChild("Door")
-- WaitForChild waits for the child to exist, if it takes too long, it throws a warning.
local DoorGamepass = 93221008 -- Put Your Gamepass Id
local Market = game:GetService("MarketplaceService")

if Door then -- Check if the Door exists before setting up the Touched event
	Door.Touched:Connect(function(hit)
		local Hum = hit.Parent:FindFirstChild("Humanoid") -- No need to do WaitForChild()
		if Hum then
            local player = game.Players:GetPlayerFromCharacter(hit.Parent) -- Moving it here, so it make sures the humanoid exists before checking for a player.
			if Market:UserOwnsGamepassAsync(player.UserId, DoorGamepass) then
				Door.CanCollide = false 
			else
				Door.CanCollide = true
			end
		end
	end)
  -- We wont need the else statement, as if it doesnt exist, it should just throw
  -- a warning message, about it being on Infinite Wait for the such object.
end
1 Like

For real i dont know why it does that.

1 Like

Yea this problem NEVER happened for me in 2017. Might of just been some engine “improvements”

1 Like

nope the script didnt work i dont know what to do now and im getting no errors.

1 Like

Where is the script located? Just making sure its not somewhere, where it will break.

1 Like

Its in StarterPlayerScripts

1 Like

That might explain why its not working, that location is meant for stuff to be executed on such players. I made a script that will be a LocalScript (so you dont have to worry about other people with the gamepass allowing people without it to enter)

Create a localscript and put it inside StarterGui

local Player = game.Players.LocalPlayer
local Door = workspace:WaitForChild("GamepassDoor"):WaitForChild("Door")
local Market = game:GetService("MarketplaceService")

local GamepassID = 93221008

-- when the player touches the door
Door.Touched:Connect(function(hit)
	local Hum = hit.Parent:FindFirstChildOfClass("Humanoid")
	if Hum then
		if game.Players:GetPlayerFromCharacter(hit.Parent) == Player then
			if Market:UserOwnsGamePassAsync(Player.UserId,GamepassID) then
				Door.CanCollide = false
			else
				Door.CanCollide = false
				
			end
		end
	end
end)

Or if you want it where it can be accessed when the player joins. (probably more recommended)

-- when the player joins
		if Market:UserOwnsGamePassAsync(Player.UserId,GamepassID) then
			Door.CanCollide = false
		else
			Door.CanCollide = false

		end
1 Like

where would i put this at because i want it accessed when the player joins. and thanks for helping me man i was so confused

-- when the player joins
		if Market:UserOwnsGamePassAsync(Player.UserId,GamepassID) then
			Door.CanCollide = false
		else
			Door.CanCollide = false

		end
1 Like

You would drop everything after the comment in the first part, where it mentions, -- when the player touches the door. Thats if you want to make it not check if the door is touched. You would just have it like this:

local Player = game.Players.LocalPlayer
local Door = workspace:WaitForChild("GamepassDoor"):WaitForChild("Door")
local Market = game:GetService("MarketplaceService")

local GamepassID = 93221008

-- when the player joins
		if Market:UserOwnsGamePassAsync(Player.UserId,GamepassID) then
			Door.CanCollide = false
		else
			Door.CanCollide = false

		end

This is how I would set it up in my experiences.

2 Likes

WAIT IT WORKED THANK YOU SO MUCH. Sorry for screaming lol im just so happy it finally worked :smiley:

2 Likes

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