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
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.
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.
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
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.