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

Can't get random value from table? [SOLVED]

Asked by 5 years ago
Edited 5 years ago

Hi! For most people, this should be easy. But I can't figure it out. I have been trying to get a random value from a table, then I want it to print. It says, attempt to call local 'SelectedGamemode' (a number value). Any help?

--Start

local G1 = "Normal"
local G2 = "FFA"
local G3 = "2 People in!"
local Gamemodes = {
G1,
G2,
G3,
}


local SelectedGamemode = math.random(1, #Gamemodes)
print('The selected Gamemode is... 'SelectedGamemode"!")

Nope. So then I try this:

--Start

local G1 = "Normal"
local G2 = "FFA"
local G3 = "2 People in!"
local Gamemodes = {
G1,
G2,
G3,
}


local SelectedGamemode = math.random(#Gamemodes) --Change here
print('The selected Gamemode is... 'SelectedGamemode"!")

Help is greatly appreciated!

0
you didn't include line 32 in there :p theking48989987 2147 — 5y
0
theking, It's probably because he did cut his script, since he doesn't need to paste it fully here. so technically it can be still in line 32. Isaque232 171 — 5y
0
No, Sorry theking and Isaque, you can just stick to the edit I made. Thanks :) LennyPlayzYT 269 — 5y

1 answer

Log in to vote
0
Answered by
Isaque232 171
5 years ago

It seems that SelectedGamemode will be a number ( max the length of the table Gamemode which would be 3 ) instead of the string you want it to be and I guess you don't want that to happen, so to get the actual gamemode name you'd need to go in the table and check what's on the value that math.random gave you

Basically for SelectedGamemode to give the actual name the variable would need to be changed to:

local SelectedGamemode = Gamemodes[math.random(#Gamemodes)]

So it would work fine, for example if math.random gave you the value 3, it would return the G3 variable which is: "2 People in!"

Also something I noticed is that you used print incorrectly, to add something to a string you should use ' .. '

print('The selected Gamemode is... 'SelectedGamemode"!")

would be:

print('The selected Gamemode is... '..SelectedGamemode.."!")

Hope that helped with your code! :D

0
Just tested the script again to make sure it was working, and it worked perfectly for me, which line are you getting this error from? Are you sure that you modified your script correctly? Isaque232 171 — 5y
0
Isaque's script should work, all you need to do is modify lines 13 and 14 to the changed versions. theking48989987 2147 — 5y
Ad

Answer this question