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

Picking a random model from folder?

This code should pick a random model from a folder in workspace but it just says its a nill value.

local Wchest = game.Workspace.Chests:FindFirstChild(math.random(1, #game.Workspace.Chests:GetChildren()))

I've tried youtube but it didn't help much.

2 answers

✓ Accepted answer

Hello! What's all the :FindFirstChild() stuff for? Just do:

local Wchest = math.random(1, #game.Workspace.Chests:GetChildren())

Also, I would just use workspace instead of game.Workspace.

Hope this helps!

:FindFirstChild() is used to search a child using a string, not a number. To get a child from a number, you can get all the children first using :GetChildren(), and beside it, put square brackets [] and inside the square brackets put a number. That will get a child from a number.

From @LikeableEmmec's answer, it is almost correct, but he forgot one thing. math.random() returns a number, so his answer will just return a number and not a model. So this one will return a model:

local Wchest = workspace.Chests:GetChildren()[math.random(1, #workspace.Chests:GetChildren())]

1 comment

0

Thank you! I did miss that lol.

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