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

How can I make the script choose one random thing?

So if I have a bunch of objects in a model called spreadable and I want it to choose one out of all these children how would I do that?

local Target = script.Parent.Spreadable

function makefire(parent)
	local fire = Instance.new("Fire")
	fire.Parent = parent
end

makefire(Target)

In the above I just said Spreadable and the script chose a random one but it continuously chose that same random one each time I ran it.

1 comment

0

You Can use math.randomseed(tick())

1 answer

You can use the method GetChildren to return to you all of the descendants of the model in a table. Then you can use the method random in the math library to pick a random index in the table:

local target = script.Parent.Spreadable
local descendants = target:GetChildren()

function makefire(parent)
	local fire = Instance.new("Fire")
	local index = math.random(1, #descendants)
	fire.Parent = descendants[index]
end

makefire(target)

Note that this function will error if #descendants is less than 1. You can use an if statement to avoid this error if necessary.

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