How do I put a word with spaces in a script

I am scripting a gui and I want to mention a frame, the frame name is Main Menu, my code is: game.Players.LocalPlayer.PlayerGui.ScreenGui.(frame name).Visible = true and I don’t know how to mention the frame Main Menu, because it has spaces.

13 Likes

Instead of doing ScreenGui.frame name do ScreenGui[“frame name”].Blah

11 Likes

You can index a child of an object with the name in a string within brackets like [“this”]. Saying object.thing is really syntactic sugar for object[“thing”], but if a child’s name has a special character in it, like a space, you have to use that way. In your case you’d do

game.Players.LocalPlayer.PlayerGui.ScreenGui["frame name"].Visible = true
17 Likes

Alternatively
game.Players.LocalPlayer.PlayerGui.ScreenGui:FindFirstChild("frame name").Visible = true

6 Likes

You should move this to Scripting Support. Code Review is for getting feedback on your code.

7 Likes

Pretty sure he used “frame name” as a placeholder for a possible name with a space in it, not as a variable name.

4 Likes

Im pretty sure that this topic should go with Scripting Support, since it allows you to get help by other devfourm members from there. Code Review is for feedback about your code based on how you did it.

3 Likes

Currently your best solution is using brackets. For example:

game.Players.LocalPlayer.PlayerGui.ScreenGui["frame name"].Visible = true

Another solution you can do is to use :FindFirstChild(“frame name”), that way you dont have to use the brackets. For example:

game.Players.LocalPlayer.PlayerGui.ScreenGui:FindFirstChild("frame name").Visible = true
4 Likes

A ton of people already answered the question several times over so I don’t think I need to be a parrot here, but I would like to point out the elephant in the room to wonder why you’re using a space for a name in the object hierarchy when the object doesn’t explicitly need a space.

It’s typically better if you don’t use spaces or whitespace characters in object names unless they’re required (e.g. NPCs). This also makes indexing and whatnot straightforward and simple. Most projects you do that involve other members, you’ll often find that spaces are not used.

local Players = game:GetService("Players")

local LocalPlayer = Players.LocalPlayer
local PlayerGui = LocalPlayer:WaitForChild("PlayerGui") -- Not implicitly available

PlayerGui.ScreenGui.MainMenu.Visible = true

Be sure also to run your code from a LocalScript. You should avoid using any kind of server-side code to manipulate or interact with user interfaces. Clients should always be responsible for their own interfaces, as such interfaces are used to facilitate input or for visuals. If anything is needed of the server, make the client make a request and let the server validate then process that request.

9 Likes

The bottom code you posted is fairly dangerous. FindFirstChild is capable of returning a nil value. If you try doing something immediately after (which in your case is setting a Visible property to true), your code will throw an error for attempting to operate on a nil value and your script will terminate.

You should always confirm existence before proceeding when using that function.

-- variables whatever

local MenuFrame = ScreenGui:FindFirstChild("frame name")
if MenuFrame then MenuFrame.Visible = true end -- Set to a single line

As a developer, if you are not creating a Gui that has its children modified at run time knowingly by your own code, then you should know that the children of the Gui will be implicitly available once the LayerCollector has been replicated from StarterGui to PlayerGui. There is only a need to ensure that the LayerCollector is present, then you can index children directly with dot syntax.

5 Likes

I feel like this point is moot, considering attempting to index a datamodel that isn’t there with dots or brackets will error similarly.

Realistically, the only real reason you wouldn’t use FindFirstChild() in this instance is because it takes longer to type, and maybe because it looks bad.

3 Likes

Which point are you talking about?

It’s not moot to confirm the existence of an object from FindFirstChild - you should be doing that. If you’re using FindFirstChild, chances are more likely that you’re unsure if something will exist when the code runs over trying to use it as a workaround for objects with spaces in their names.

The primary point of my response is mentioning that trying to run something directly after using FindFirstChild is not a smart idea because if FindFirstChild returns a nil, then the code will throw an error, hence my addition to the code that checks if MenuFrame is not nil before attempting to set visibility.

As for the second point, it’s a given that dot syntax will also throw an error if it doesn’t exist the same way operating on a nil value returned from FindFirstChild will. The difference is that I’m saying if you have a static Gui that has minimal to no changes at runtime and is inserted as-is, chances are that the Gui’s object hierarchy won’t be modified. Cloning is a synchronous operation, so the children of the LayerCollector will be implicitly available when the LayerCollector itself has finished cloning from StarterGui to the PlayerGui.

3 Likes

I am operating purely in the context of the OP, and for his problem, FindFirstChild and bracket syntax is essentially interchangeable.

I am not saying that FindFirstChild is useless, but for the OP’s use case, it makes no difference, as they were simply asking how to index an object with spaces in its name.

6 Likes

You have to index it by a string or a function. I’d suggest using the function:

[Instance]:WaitForChild("Main Menu")

This function stops the script and waits until it finds the child in the instance. But there can be warnings such as:

-- Warn: infinite yield on '[Place Name]:WaitForChild([Child Failed to Find])' 

This is also the same as “script timeout” error.

Other ways you can do this is using

[Instance]:FindFirstChild() -- Finds the child inside the instance.
[Instance]:FindFirstChildOfClass() -- Finds the first child inside the instance.

Or you can simply do this script

game.Players.LocalPlayer.PlayerGui.ScreenGui["Main Menu"].Visible = true
1 Like

Your Original Script:
game.Players.LocalPlayer.PlayerGui.ScreenGui.(frame name).Visible = true
The Fixed Verison:
game.Players.LocalPlayer.PlayerGui.ScreenGui:FindFirstChild("the name of your frame").Visible = true

Couldnt you do

game.Players.LocalPlayer.PlayerGui.ScreenGui["Frame name"].Visible = true

???

1 Like