Random from Table

  1. What do you want to achieve? I’m trying to get a random item from a table. (Please note, as in Virus I mean a Biological Virus)

  2. What is the issue? I can’t find a Random function to do that

  3. What solutions have you tried so far? Use Random.new() and it said it need a number and not a table

Here is the Code:

Script Inside A Model

local sympts = {

}
local viral = require(game.ReplicatedStorage.VirusScript)
local symot1 = viral:GetRandom()
table.insert(sympts, 1, symot1)


ModuleScript Inside Replicated Storage

local virus = {}
function Color()
	
end
function Scars()
	
end
function Walk()
	
end
function Zombie()
	
end
function Bleed()
	
end
function Fire()
	
end
local Symptoms = {
colorChange = Color(),
Scars = Scars(),
Walk = Walk(),
zombiefi = Zombie(),
Bleeding = Bleed(),
Cumbustion = Fire(),

}
function virus:GetRandom()
	print("First Sympt")
	local FirstSympt = Random.new(Symptoms) --This is where it is erroring out
	print(FirstSympt)
	return FirstSympt
end


return virus

1 Like

I’m pretty sure this should work:

local FirstSympt = Symptoms[math.random(1, #Symptoms)]

It picks a random index from 1 to the number of values in the table (max number). Arrays start at index 1 on Roblox.

1 Like

It didn’t Work, Here’s the Error Code
ReplicatedStorage.VirusScript:31: bad argument #2 (interval is empty)

If that’s the case there is something wrong with your array. I’d recommend watching some videos on arrays

Could it be Because it is technically a dictionary? I tried Instead of #Symptoms with a integer and it worked. But it is now printing out nil

Just Did some tests and Yes, It is Because it is Technically a Definition and you can’t receive the amount in it through #Symptoms

Got it to work, Just Changed it to an array and it is now working perfectly!

Yes. You can’t get the size of a dictionary using the hashtag integer.