I am currently working on a feature that requires a local script to be able to detect instances in the workspace. For clarification, the Local Script is located in PlayerGui and must be able to detect certain folders in the workspace in a loop. For example:
--Local script: game.Players.LocalPlayer.PlayerGui.LocalScript
local Map1 = game.Workspace:FindFirstChild("Map1")
local Map2 = game.Workspace:FindFirstChild("Map2")
while wait(1) do
if Map1 then
print("Map1 is in workspace")
if Map2 then
print("Map2 is in workspace")
The issue is that the (print) function is not working. Any help is greatly appreciated.
while wait(1) do
if game.Workspace:FindFirstChild("Map1") then
elseif game.Workspace:FindFirstChild("Map2") then
What is probably happening is that the script is running before the map objects are loaded in so the
Map2 variables are starting out as nil. this means that every if statement is
if nil then which always will be false.
You can do two things. 1st is do what @rfisty said and use
:FindFirstChild in the loop, or you can change the
:FindFirstChild in your code to
:WaitForChild which will make the script pause until the map object is found.
By this do you mean changing the variable names to:
local Map1 = game.Workspace:WaitForChild("Map1")
Yes. That way it will wait until the map object is found, then the script will continue on.
This seems to result in a infinite yield error, probably because the map entering workspace isn’t a sure thing. Maps are only added one at a time and then removed after a few minutes, meaning either Map1 or Map2 can be active at a time, not both. Do you know if there is a way to detect which map is active from a local script?
You are getting the Infinite yield because the map is not part of the workspace when you are looking for it. The easiest and simplest way to do a check is to just make the variable check
:FindFirstChild() in the while loop as in the first reply. That way you are re-checking if the map exists every time that the loop is run instead of just when the script first runs.