Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I use a variable that contains spaces in FindFirstChild()?

Asked by
TTHKKB 12
4 years ago

Hello, I am trying to make a script that can find the tool in the ReplicatedStorage. And I wanted to use a StringValue with the value of the tool's name, but I don't know how to do it. Can someone help me, please?

This is the script I use:

local WepName = script.StringValue.Value

local Weapon = game.ReplicatedStorage:FindFirstChild(WepName)

But the problem is, the Value of the StringValue contains spaces. For example, the Value is called "Imperial Gold Sword". It breaks the FindFirstsChild thing and I don't know what to do. Someone help me, please. (Sorry about my bad English, it's not my first language.)

0
FindFirstChild() shouldn't break because of whitespaces in a string. Have you checked to see if the sword is a child of ReplicatedStorage rather than a descendant? DeceptiveCaster 3761 — 4y
0
You should name the tool with no space and I think you don't need to use a StringValue for this. Block_manvn 395 — 4y
0
But I wanted to make the data easier to edit cause I got lots of these stuff, and the tool name has to contain spaces or it will look weird in the game. TTHKKB 12 — 4y
0
Spaces should not break the function. The issue should lie under the assumption that the sword is not a child of ReplicatedStorage. DeceptiveCaster 3761 — 4y

2 answers

Log in to vote
0
Answered by 4 years ago

I don't think the issue is related to the name of the weapon. Here's why.

Recall FindFirstChild()'s two parameters:

FindFirstChild(name, recursive)

Argument 1, which gets assigned to name, should be a string. Strings are allowed to have whitespaces; variable names are not.

Argument 2, which gets assigned to recursive, should be a boolean. This parameter determines if the function will search recursively, meaning it will search through descendants instead of just children. By default, this is set to false.

Since this doesn't work...

FindFirstChild("Imperial Gold Sword")

...why not do this instead?

FindFirstChild("Imperial Gold Sword", true)

Note that you have to make sure that the weapon is actually named Imperial Gold Sword and does not contain any extra, missing, or incorrect characters.

0
Thanks for your answer, it’s really helpful. But it’s a variable that I put inside the brackets, AND the String that the variable defines contains spaces, that’s what break the script. By the way, I checked and I’m sure that the Imperial Gold Sword is a child of ReplicatedStorage. TTHKKB 12 — 4y
0
Have you tried using WaitForChild() instead? DeceptiveCaster 3761 — 4y
0
TTHKKB you're useless programmerHere 371 — 4y
0
Oh, I haven’t tried WaitForChil() I’ll try it later on when I get home and I’ll tell you if it works or not. Thank you so much! :D TTHKKB 12 — 4y
0
The WaitForChild() didn't work too. I think it's the problem of other parts of the script. Thank you for your answer though, it's really helpful! :D TTHKKB 12 — 4y
Ad
Log in to vote
0
Answered by
haba_nero 386 Moderation Voter
4 years ago

Hello! What I would suggest is using this symbol _ to represent spaces. It's not as pretty, but it does work! So you would set the value to Imperial_Gold_Sword instead of Imperial Gold Sword

This is the best I can do. I hope this helps you!

Best,

TheLastHabanero

0
The name is represented by a string, not a variable. Using underscores would be entirely irrelevant. DeceptiveCaster 3761 — 4y

Answer this question