Choosing a random array

Currently I’m working on a problem to choose a random array. I had a working system but it tended to be extremely faulty and honestly not the greatest at all.

I’d like to have a system where I can select the items within the random array as well… For example

UsersRandomArray[1] = “Blah blah” etc…

local Diagnostics = { -- ['Name'] = Symptom1, Symptom2, LeastPainlevel, MostPainLevel
	['Common Flue'] = {"Mild Vomiting","Headache and Nausea",1,5};
	['Allergic Reaction'] = {"Severe Cough","Swollen Eyes",1,4};
	['Strep Throat'] = {"Trouble Breathing","Sore Throat",3,6};
	['Stomach Bug'] = {"Vomitting or Harsh Coughing","Stomach Pains or Cramps",2,5};
	['Jaundice'] = {"Fatigue or Nausea","Vomitting and weight loss",3,9};
	['Pneumonia'] = {"Fever or Sudden Chills","Dehydration or Appetite loss",4,9};
	['Mild Rash'] = {"Itching Skin","Bumps and Red Spots",1,4};
	['Severe Rash'] = {"Severe Itching","Bleeding and White Rashes",2,6};
	['Lung Buildup'] = {"Trouble Breathing","Severe Coughing",3,7};
	['Ebola'] = {"Severe Bleeding in multiple locations","Loss of vision",8,10};
	['Common Cold'] = {"Cough or Runny Nose","Headache and Nausea",1,5};
	['Heart Failure'] = {"Seizures or Cramps","Dehydration",8,10};
	['Broken Arm'] = {"Trouble moving arms","Trouble lifting objects",6,9};
	['Broken Leg'] = {"Trouble walking or running","Trouble moving legs",6,9};
	['Fractured Rib'] = {"Cramps and Sharp Pains","Trouble bending over",7,10};
	['Brain Tumor'] = {"Severe Migraines","Trouble Sleeping",8,10};
	['Strokes'] = {"Random loss of nerves","Stroking or loss of balance",7,10};
	['Chicken Pox'] = {"Rashes or pimples on body","Severe Itching",3,7};
	['Leukemia'] = {"Pain in bones or joints","Easily brusing",5,9};
	['Asthma Attack'] = {"Hard to breath","Severe coughing",3,7};
	['Lupus'] = {"Pain in the muscles","Sharp chest pain",4,10};
};

So ideally I could put this in a localscript, it’d give the user a certain diagnostic… then I could set the diagnostic symptoms to a textlabel etc…

Ex:

TextLabel.Text = UsersDiagnostic[1]

I attempted this----- and it returned nil

local UsersDiagnostic = Diagnostics[math.random(1,20)]
print(UsersDiagnostic)
1 Like

Have you tried this?

local usersDiagnostic = Diagnostics[math.random(1, #Diagnostics)]
-- More code

Just btw, that only works for tables that use incrementing indexes. You’re using strings as your indexes, so you can’t really do this O(1) operation for a random element.

2 Likes

I have tried that. It returns the same error as always

https://gyazo.com/4a8e28f0111f3d9dcaed63d4398f56ac

In order to get a random element from a dictionary, you’ll need to list the elements numerically first, and then choose a random element from that numerical array.

local function ChooseRandom(dictionary)
	local list = {}
	for key, value in pairs(dictionary) do
		list[#list+1] = {key = key, value = value}
	end
	if #list == 0 then return end
	return list[math.random(#list)]
end

local chosen = ChooseRandom(Diagnostics)
print("Name: "..chosen.key)
9 Likes

Hm. This was nearly what I had before! I’ll bug test this and make sure it works how I want it. Thank you!

Sending the information “chosen” to the server is causing an error or prints my name
1st: game.ReplicatedStorage.GetCash:FireServer(player,chosen)

game.ReplicatedStorage.GetCash.OnServerEvent:Connect(function(player,chosen)
if chosen then
print(chosen)
end
end)

OUTPUT: Anton00k (LocalPlayerName)

2nd: game.ReplicatedStorage.GetCash:FireServer(player,chosen)

game.ReplicatedStorage.GetCash.OnServerEvent:Connect(function(player,chosen)
if chosen then
print(chosen.key)
end
end)

OUTPUT: key is not a valid member of Player

Whole Script:

local function ChooseRandom(Amounts)
	local list = {}
	for key, value in pairs(Amounts) do
		list[#list+1] = {key = key, value = value}
	end
	if #list == 0 then return end
	return list[math.random(#list)]
end

while wait() do
local chosen = ChooseRandom(Amounts)
print(chosen.key)
game.ReplicatedStorage.GetCash:FireServer(player,chosen)
wait()
	local randomwait = math.random(5,15)
	print("Wait: "..randomwait)
	wait(randomwait)

end
1 Like

The client who fires a RemoteEvent is passed implicitly, don’t include an argument for it in FireServer but define it as the first parameter for OnServerEvent. In OnServerEvent, the player who fired the remote is passed first and all variables are shifted down one. That means chosen gets discarded and you’re only left with a redundant player variable.

-- Client
local GetCash = game:GetService("ReplicatedStorage"):WaitForChild("GetCash")
GetCash:FireServer(chosen)

-- Server
local GetCash = game:GetService("ReplicatedStorage").GetCash

GetCash.OnServerEvent:Connect(function (player, chosen)
    if chosen then
        print(chosen) -- or chosen.key
    end
end)
3 Likes