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

How would I go about selecting a random string between a set of strings?

Asked by
Spookyse -11
5 years ago

Fairly new to scripting; I wanted to see if you could select a random string between a set of strings, such as ("Azael, Construct, Cameo") and then it would select one of the strings randomly and then it would save that as your race

tl;dr Can anyone make a script on selecting a random set of strings that I can set and then it would save your race throughout different servers.

3 answers

Log in to vote
0
Answered by
Xapelize 2658 Moderation Voter Community Moderator
5 years ago

You can make math.random and choose a random name.

Let's get right into the script! :3

local num = math.random(1,3)

Let use 'num' be an example. The 'num' will choose 1 to 3 randomly.

if num == 1 then
    print("Azael is chosen.")
end

Then if 'num' was 1, the output will prints 'Azael is chosen.'

if num == 2 then
    print("Construct is chosen.")
end

If 'num' was 2, the output will prints 'Construct is chosen.'

if num == 3 then
    print("Cameo is chosen.")
end

Finally, if 'num' was 3, the output will prints 'Cameo is chosen.'

Easy, right? If you later need a random string or anything, you can use math.random(min,max) to make easier scripts.

I hope you have a nice day and if you have any questions, comment below! Bye! :3

Ad
Log in to vote
0
Answered by
proo34 41
5 years ago

Jack_Roblox has a good answer, but it's clunky. Use elseif statements.

local chosen = math.random(1,3)

if chosen == 1 then
--code here for 1
print("Azael " .. chosen)
--end of code 1
elseif chosen == 2 then
--Code here for 2
print("Construct " .. chosen)
--end of code 2
elseif chosen == 3 then
--Code here for 3
print("Cameo " .. chosen)
--End of code 3
end

elseifs are awesome for this, as this is what they are made for. Where the print is, that is where you will put code.

Log in to vote
0
Answered by
Robowon1 323 Moderation Voter
5 years ago

you guys both have clunky answers, just do:

local random = math.Random(1,3)
local strings = {"Azael", "Construct", "Camero"}
print(string[random].." was chosen")

Answer this question