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

Selecting a random child Not Working?

Asked by
zane21225 243 Moderation Voter
3 years ago

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!

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. zane21225 243 — 3y
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. zane21225 243 — 3y

2 answers

Log in to vote
1
Answered by
Elyzzia 1294 Moderation Voter
3 years ago

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

Ad
Log in to vote
0
Answered by 3 years ago

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!

Answer this question