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

Picking a random model from folder?

Asked by 1 year ago
Edited 1 year ago

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

Log in to vote
1
Answered by 1 year ago

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!

Ad
Log in to vote
0
Answered by 1 year ago

: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())]
0
Thank you! I did miss that lol. LikeableEmmec 470 — 1y

Answer this question