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

Selecting a random child Not Working?

Hello all, I am working on a game script, in which the script chooses a random Part out of the "RoundSpawns" Folder.

I've used this exact same line of code for years, but all of a sudden it seems to be not working.

player.Character.HumanoidRootPart.CFrame = workspace.RoundSpawns[math.random(#roundTeleportsGC)].Position + Vector3.new(0, 5, 0)

Below is what it shows in the Output.

6 is not a valid member of Folder

Thanks in advance!

2 comments

0

I forgot to mention that I've looked at some DevForum posts and they seem to be using the exact same method as me, but it works for them.

0

Another thing to note is that sometimes it dosen't even say the correct number of items in the folder! There are 6 random teleports in the folder. Sometimes it'll show it as 2 or 3 for some reason.

2 answers

✓ Accepted answer

when you do workspace.RoundSpawns[math.random(#roundTeleportsGC)], it attempts to look for a child or property in the RoundSpawns folder literally named that number (as a number, not a string)

you probably meant to do roundTeleportsGC[math.random(#roundTeleportsGC)] instead

Maybe you should consider grabbing the table of parts inside the folder and just indexing. This will allow for less errors and you can name the parts inside the folder whatever you like:

local RoundSpawns = workspace:WaitForChild('RoundSpawns')

local list = RoundSpawns:GetChildren()
--returns a list of all children in RoundSpawns

local randomPart = list[math.random(#list)]
--we access the list, randomly indexing from 1 to the list's length

Character:MoveTo( randomPart.Position + Vector3.new(0, 5, 0) )
--we apply the random part to the character's position

This is assuming the folder only contains instances that hold a Position property.

Good luck!

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