local clickDetector = game.Workspace.Pages.Page1.ClickDetector
if not clickDetector then
error("ERROR: clickDetector not found in Destroy Script.")
end
clickDetector.MouseClick:Connect(function(player)
game.Workspace.Pages.Page1:Destroy()
end)
So this is the current script.
What I first want to do is change how we obtain the clickDetector. We can simplify this a lot by doing the following:
local pageList = script.Parent
local page1 = pageList:WaitForChild("Page1", 5)
if not page1 then
error("ERROR: Could not find Page 1.")
end
local clickDetector = page1:FindFirstChild("ClickDetector")
What we do here is the following:
- First we split up the variables into multiple variables. This makes reusing them easier
- We use :WaitForChild to make sure it yields until it finds the page, or if 5 seconds have passed. The check after makes sure it errors and stops the script if it didnt find it
- Then in the final line we just get the clickdetector
Replace your first line of code with this