Picking a random variable through math.random returns all variables?

I’m working on a “plates” type game. A random plate is selected (this works), and that random plate then has an “effect” added to it (this is where the issue is).

I’ve written this code, that, in theory, should work:

function SelectPart()
	local randomPlate = Plates[math.random(1, #Plates)]
	
	-- [MAKING THE ATTACKS]
		local function LavaAttack()
			-- In the future, I'd turn this into a tween
			randomPlate.Size = Vector3.new(9, 10, 9)
			randomPlate.BrickColor = BrickColor.new("Really red")
			-- Set player health to 0
		end
		local function SpinAttack()
			-- In the future, I'd turn this into a tween
			print("Spin")
		end
		local function Shrink()
			-- In the future, I'd turn this into a tween
			randomPlate.Size = Vector3.new(2, 2, 2)
		end
		local function Cage()
			-- In the future, I'd turn this into a tween
			print("Cage")
		end
	
	
	-- [Play a random attack on this plate]
	local Functions = {Cage(), Shrink(), SpinAttack(), LavaAttack()}
	local ChosenFunction = Functions[math.random(1, #Functions)]
	
	ChosenFunction()
end

SelectPart()

Instead of choosing one random function from a table of four functions, it instead runs all four functions. I am also getting an error, but the functions are still being run:

ServerScriptService.Gameplay.MainGameplayModule:34: invalid argument #2 to ‘random’ (interval is empty) - Server - MainGameplayModule:34

All help is appreciated!

1 Like
  • Adding the brackets after listing each function will run the function itself and use the return value, you should not include the brackets for storing them.

It’d be better to have each function seperate and pass the plate as a parameter to it.

2 Likes

if @12345koip 's solution doesn’t work (which I doubt will happen), perhaps you could implement something similar to this:

1 Like

This means your table has no entries. If we do math.random(1, 1), we get 1, but if we do math.random(1, 0) we get the same error that you got. Try printing Functions onto your console.

Try this

function SelectPart()
	
	-- [MAKING THE ATTACKS]
		local function LavaAttack()
			-- In the future, I'd turn this into a tween
			-- Set player health to 0
         print("LavaAttack")
		end
		local function SpinAttack()
			-- In the future, I'd turn this into a tween
         print("SpinAttack")
		end
		local function Shrink()
			-- In the future, I'd turn this into a tween
			print("Shrink")
		end
		local function Cage()
			-- In the future, I'd turn this into a tween
			print("Cage")
		end
	
	-- [Play a random attack on this plate]
	local Functions = {Cage, Shrink, SpinAttack, LavaAttack}
	local ChosenFunction = Functions[math.random(1, #Functions)]
	
	ChosenFunction()
end

image
@real_bzg0

2 Likes

This is happening because rather than assigning the functions as variables to the table, you are calling them inside of it.

The fix should be really easy, just remove the brackets from the functions when you assign them to the table so that they’re assigned as variables properly.

local Functions = {Cage, Shrink, SpinAttack, LavaAttack}

note: I accidentaly replied to someone else, the reply was meant for the OP, sorry.

3 Likes

It is because you are running the functions immediately inside the array, instead do so:

	local Functions = {Cage, Shrink, SpinAttack, LavaAttack}

Everything else looks good and do not needs any changing.

1 Like

@real_bzg0

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