generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: How could I execute a function from a string

Previous Thread :: Next Thread 
helloburp is not online. helloburp
Joined: 26 Aug 2011
Total Posts: 14376
27 Jun 2017 04:42 AM
like say I have a user input thing in which players enter the name of a function. Implying this is sent to a script with functions of the same name defined, how could I fire a function with the name of the string without using if statements?
Report Abuse
JarodOfOrbiter is not online. JarodOfOrbiter
Joined: 17 Feb 2011
Total Posts: 20029
27 Jun 2017 04:44 AM
Use a table.

local functions = {test = function() print("Test") end}


chat = "test"
functions[chat]()


Report Abuse
helloburp is not online. helloburp
Joined: 26 Aug 2011
Total Posts: 14376
27 Jun 2017 04:46 AM
I'd like to do it without any external things, only the string and the plugin. Like even if I made a new function and didn't enter it into some table or define it in any other way, I could still run it with just the string being parsed.
Report Abuse
helloburp is not online. helloburp
Joined: 26 Aug 2011
Total Posts: 14376
27 Jun 2017 04:47 AM
Although if I need to use a table I can live with it, lol
Report Abuse
JarodOfOrbiter is not online. JarodOfOrbiter
Joined: 17 Feb 2011
Total Posts: 20029
27 Jun 2017 04:48 AM
So you aren't using it to call a function, you're using it to create a function?

I (And everyone else) highly recommend against it. It's not a good practice, and should never ever be necessary. Ever.

It is called 'loadstring' and shouldn't be used, it can only be used in a Script and only if LoadstringEnabled is on.


Report Abuse
JarodOfOrbiter is not online. JarodOfOrbiter
Joined: 17 Feb 2011
Total Posts: 20029
27 Jun 2017 04:49 AM
For example

chat = "print('Hello, world!')"
newFunction = loadstring(chat)
newFunction()


Report Abuse
helloburp is not online. helloburp
Joined: 26 Aug 2011
Total Posts: 14376
27 Jun 2017 04:53 AM
No, I am implying that there are already functions and that a string that is the same as the function's name would call it

function potato()

end

input = "potato"

--call function that matches input, so call potato

--???

--profit

but tables seem to make sense for this, if there is a better way then let me know. And I am aware of the dangers of the class that must not be named
Report Abuse
JarodOfOrbiter is not online. JarodOfOrbiter
Joined: 17 Feb 2011
Total Posts: 20029
27 Jun 2017 04:57 AM
There is one other way then.

If you don't use local variables and functions, you can use getfenv.



function potato()

end

-- All your functions above this line
fenv = getfenv() -- 'fenv' stands for function environment
-- fenv holds all of the global variables in your code.

input = "potato"
f = fenv[input] -- get the function
if type(f) == "function" then -- So we don't try to call something that isn't a function
f()
end


Basically it's the same thing, but the table is generated automatically (Or whatever, the table you use is the script's global environment. Ykwim)


Report Abuse
emperormicah is not online. emperormicah
Joined: 31 May 2011
Total Posts: 3052
27 Jun 2017 04:58 AM
function exe(str,...)
if getfenv()[str] then
return getfenv()[str](...)
end
end

exe'warn'
Report Abuse
helloburp is not online. helloburp
Joined: 26 Aug 2011
Total Posts: 14376
27 Jun 2017 04:59 AM
Awesome, that's much cleaner and exactly what I was after. Thanks
Report Abuse
JarodOfOrbiter is not online. JarodOfOrbiter
Joined: 17 Feb 2011
Total Posts: 20029
27 Jun 2017 05:03 AM
If you're making a plugin you can also use this nifty doodad.



local pluginFunctions = {}
function pluginFunctions.selectRandom() -- example function
local children = game.Selection:Get()[1]:GetChildren()
game.Selection:Set(children[math.random(#children)])
end

function _G.initialize()
for i, v in pairs(pluginFunctions) do
getfenv(2)[i] = v
end
end


Now, when you call '_G.initialize' from the command bar, all of the functions in pluginFunctions become default functions in the command bar:

_G.initialize()
selectRandom() -- ololol no error


Report Abuse
Exeplex is not online. Exeplex
Joined: 29 May 2008
Total Posts: 1081
27 Jun 2017 05:15 AM
I use a protected call and hand it whatever is being chatted.

local function RunFunction(FunctionToRun)

local Success = true;
local Func = nil;

Success = pcall(function() Func = FunctionToRun() end);

if Success then
print("Function ran successfully!");
else
print("Function has encountered an error.");
end

return Success;
end


RunFunction(function() print("Hey") end);
Report Abuse
Exeplex is not online. Exeplex
Joined: 29 May 2008
Total Posts: 1081
27 Jun 2017 05:17 AM
But this isn't fully related to the post, sorry!
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image