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

Selecting a random value?

Asked by 10 years ago

I would like to make a coding script that picks a random StringValue located inside another StringValue called Options. This is the code I have, but it is only picking the first parent inside Options.

script.Parent.Changed:connect(function(val)
if val == true then
local list = script.Parent.Parent.Parent.Parent.Options:GetChildren()
local m = list[math.random(#list)]
local n = math.random(1,2)
if n == 2 then
-- Coding here that has nothing to do with other functions
elseif n == 1 then
-- Coding here that has nothing to do with other functions  
end
val = false
end
end
end)
0
`m` should definitely be a random child of `Options` -- can you be more specific with why you think there's a problem? (The code you posted doesn't use `m` so I'm guessing somewhere else along the way you have a mistake) BlueTaslem 18071 — 10y

1 answer

Log in to vote
0
Answered by 9 years ago

Here's where you have done your mistake.

While selecting a random value you will have to use math.random(from, to), You did used it, but not correctly.

For example, If I wanna choose a number btw 1 to 10 in a table like this.

local numbers = {1,2,3,4,5,6,7,8,9,10}

What I would do is

local numbers = {1,2,3,4,5,6,7,8,9,10}

local random = numbers[math.random(1, #numbers)] -- Here, I am selecting the table called numbers and then selecting a random number

Here's how the script should look like

script.Parent.Changed:connect(function(val)
if val == true then
local list = script.Parent.Parent.Parent.Parent.Options:GetChildren()
local m = list[math.random(1,#list)]
local n = math.random(1,2)
if n == 2 then
-- Coding here that has nothing to do with other functions
elseif n == 1 then
-- Coding here that has nothing to do with other functions  
end
val = false
end
end
end)

Hope that helps :D

Ad

Answer this question