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.
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())]