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
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
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.
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)
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
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
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