Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

repeat wait() until [object]?

So im trying to make a thing where it requires to change the cam to a part at first but im having the problem the script is firing before the game is loaded so I tried this:

repeat wait() until game.Workspace.MorphGui.CamPart
repeat wait() until game.Workspace.MorphGui.DevTrooperSpot

The thing is it is saying "CamPart is not a valid member of Folder "Workspace.MorphGui"" So idk whats going on. Can someone please help?

1 comment

0

You can use game:Loaded() and then maybe use a global variable like _G if your script is somewhere else

2 answers

✓ Accepted answer

The reason its failing is if you string a bunch of objects into what Roblox calls a GetFullName() (EG: "game.workspace.part.script"). Unless all the parts are there already on script run then things would load normally and not error out.

But if something loads slower than the script or fails to load by the time your script has got to the part where you require the object it will fail and return nil as you know.

So to try and keep things happy i would make sure each object exists before continuing, so I've made up 2 ways on how to accomplish this. There are many ways on doing this and some a much neater than these ways, but the two ways should suffice to your needs.

The number 2 way, also saves the objects in a value for you to use later on Via CameraBase and DevTrooperSP

Try these ways:

--// 1) long winded way!
local CamPartLoaded = false

repeat
	local Group = game.Workspace:FindFirstChild("MorphGui")
	if(Group == nil) then error("MorphGui is nil and not a valid member of workspace!!") end
	CamPartLoaded = Group:FindFirstChild("CamPart") ~= nil
	wait() 
until CamPartLoaded == true

local DevTroopSpotLoaded = false
repeat 
	local Group = game.Workspace:FindFirstChild("MorphGui")
	if(Group == nil) then error("MorphGui is nil and not a valid member of workspace!!") end
	DevTroopSpotLoaded = Group:FindFirstChild("DevTrooperSpot") ~= nil
	wait() 
until DevTroopSpotLoaded

--// 2) Quick and Neater way
local Group = game.Workspace:FindFirstChild("MorphGui")
if(Group == nil) then error("MorphGui is nil and not a valid member of workspace!!") end
local CameraBase = Group:WaitforChild("CamPart",30)
local DevTrooperSP = Group:WaitforChild("DevTrooperSpot",30)

--[[
The only way the above no 2)way will break is if the structure of your game is changing during run time,
If it does then use No 1) way!

]]

There are many ways to wait for something to load on game run or even while the game is running. If you require the script in question to always run thus always search for the object(s) during playtime then i would use no "1)" way.

Hope this helps!

Edit: typos and stuff :p

You don't have to do repeat wait() until. You can use WaitForChild("")

so instead you do this:

local MorphGui = game.Workspace:WaitForChild("MorphGui") 
MorphGui:WaitForChild("CamPart") 
MorphGui:WaitForChild("DevTrooperSpot")

WaitForChild("InsertItemHere") is basically you telling the game to wait until said thing loads up. The game will not execute the script Until what you told it to wait for appears. The reason why it said CamPart isn't a Member is because it doesn't see an item that has the name. WaitForChild doesn't care about this issue. So use WaitForChild instead.

tell me if it doesn't work.

This discussion is preserved as it was. New answers, comments, and votes are closed.