Math.random Improvements [RandomMode]

Hello!
So basically I have this script that triggers a few events based on RandomMode value, I’d like to ask how can I improve this without having to type so many times if RandomMode ==

To be more specific, I’d like to make something more simple. Like… instead of using so many or RandomMode ==, I would preffer putting all of the numbers once… like:

local = RandomMode = math.random(1,30)

--[[
 Common event N1 happens when numbers: "1,2,4,5,6,7,8,9,11,12,13,14,15,16,17,18" are selected

In that way but more simplified, i hope you guys understand what I mean by that. I'm asking this because I'm planning into adding more numbers into "math.random" and I want to prevent doing more "or RandomMode =="
--]]

Here is the script that I’m currently using.

local RandomMode = math.random(1,30)

task.wait(2)
print(RandomMode.. " has been selected.")

Stun.Changed:Connect(function()
if script.Parent.NPC.Health > 0 then
	if RandomMode == 1 or RandomMode == 2 or RandomMode == 4 or RandomMode == 5 or RandomMode == 6 or RandomMode == 7 or RandomMode == 8 or RandomMode == 9 or RandomMode == 11 or RandomMode == 12 or RandomMode == 13 or RandomMode == 14 or RandomMode == 15 or RandomMode == 16 or RandomMode == 17 or RandomMode == 18  then	
		-- common event
	elseif RandomMode == 3 then
		-- event
	elseif RandomMode == 25 then
	-- event

		-- Bruh Hour
	elseif  RandomMode == 19  then
		-- event

	elseif RandomMode == 10 or RandomMode == 20 or RandomMode == 21 or RandomMode == 22 or RandomMode == 23 or RandomMode == 24 or RandomMode == 26 or RandomMode == 27 or RandomMode == 29 then
	--  common event
	elseif RandomMode == 30 then
		-- event
	elseif RandomMode == 28 then
	-- event 
		end
	end
end)

Let me know if you know how can i do that.

Just use a table with numbers/functions then use table.find then get

Thanks for the advice but I’m not sure how do I do that… can you explain it to me? Like a script example, you don’t need to make the whole script.

by using tables like this

local modes = {
[1] = print("1"),
[2] = print("2")
}
local random = math.random(#modes)
if script.Parent.NPC.Health > 0 then
if random == modes[random] then 
modes[random]
end
end

you dont need the if statement but yeah, make it better than mine though

Ok, thanks but I apologize but can you explain me what is that script and what it does? I’m really sorry about not understanding something like this, It’s new for me.

Also adding to my comment, is it normal to have this line marked as red?
image

Yeah sure let me load up my pc

1 Like

modes would be the table that has been created which contains numbers that specfically gives a code for each.

local RandomMode = math.random(1, 30)
task.wait(2)
print(RandomMode.. " has been selected.")

local set1 = {1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18}
local set2 = {10, 20, 21, 22, 23, 24, 26, 27, 29}

Stun.Changed:Connect(function()
	if script.Parent.NPC.Health > 0 then
		if table.find(set1, RandomMode) then
			--[[ common event]]
		elseif table.find(set2, RandomMode) then
			--[[  common event]]
		elseif RandomMode == 3 then
			--[[ event]]
		elseif RandomMode == 25 then
			--[[ event]]
			--[[ Bruh Hour]]
		elseif  RandomMode == 19  then
			--[[ event]]
		elseif RandomMode == 30 then
			--[[ event]]
		elseif RandomMode == 28 then
			--[[ event ]]
		end
	end
end)

Here you go mate, this is an example but it fits on what you want to achieve
You will still need some elseifs but this reduced it by ALOT (ive also already checked the numbers where they’re meant to go)

Thank you very much man, I was expecting only a explanation but that helped me a lot.

Anytime, anything else? I have to eat in a few

I’ll test the script, nothing else right now, have a nice day.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.