Call function with variable (using AeroGameFramework)

I am using Crazyman32’s AeroGameFramework, and I am creating a round based game. I am trying to call a function that is identified in the :Start() area of the round service with a string stored in a variable.
Here is the code I tried:

local event = events[math.random(1,#events)]
[event](player)

“events” is a table containing 4 strings, and each function is named after each string in the table.
I get the error ServerStorage.Aero.Services.RoundService:54: attempt to call a nil value

How do I call the function using the string stored in the variable “event”?

1 Like

I don’t think you would need the square brackets on the second line:

local event=events[math.random(1,#events)]
event(player)
1 Like

Not sure if that would work if it’s a string in the table, maybe?

I removed the square brackets, however I got the same error.
14:26:39.956 - ServerStorage.Aero.Services.RoundService:63: attempt to call a string value
(Different line because I added code above that block in the script)

Can you show us what the events variable is?

Just a table full of strings.

local events = {"Die","ColorPlate","Explode","Slow"}

Well that’s your issue. You’re literally trying to call a string value. Let’s say the result of math.random(1, #events) ends up being 3 then what the event variable ends up being is "Explode".

Do you have a folder in ServerStorage or ReplicatedStorage which has all your RemoteEvents?

Yes, and then that’s my question. How do I use a string value to call a function named the same thing as the value of the string?

This is all in a ServerScript created with AeroGameFramework, and I have no events besides one that updates the status bar at the top of the screen (a text label)

Are your events such as “Slow” inside that same script or is it a global function?

You should store the functions themselves in the table, not strings.

What you’re asking isn’t possible by any practical means.

Can you possibly show us your entire script or provide an .rbxl? You can’t call a function by string.

1 Like

“events” is simply the table name, not the actual object. They are all functions specified in the script.

I can try to just store the functions in the table itself, instead of strings.

1 Like

Yes we get that,but when you do #events your counting everything inside the table and going by 1 to the number of things in the table and selecting 1 thing at random

What is your goal with this system in the first place? Are you trying to randomly pick a function and run it?

1 Like

Yes, that is what I’m trying to do.

1 Like

Because a data store is irrelevant here. OP isn’t trying to store data that is persistent through game sessions.

3 Likes

Technically you could call a function if it was _G, but since this is all in the same script I dont think it would be possible.

heres how you can link your functions into the events table

local events = {}
events.Explode = function ()
	-- Code here
end
1 Like

It sounds like an event system for a minigame where something random happens to a player and he is trying to randomize what happens.

Here is an example that would work for randomizing events (this is a module per event basis as it would become more organized than having every event inside a table):
Example.rbxl (15.3 KB)

@helperobc’s method would work as well though.

2 Likes

I tried using this, however I get the interval empty error.

How do I get a random function from this example?

1 Like