Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Is it possible to execute a variable?

Pretty much the title.

For example:

part = -- lets say this is unknown, and it was defined some other place. just ignore this line
theOtherCommand = -- also unknown

My question is, how do I execute "theOtherCommand"?

To clarify, let's say

theOtherCommand = part.CanCollide = false

I wouldn't know the value though, but I still need to execute it. The value of theOtherCommand came from something that could have made the value anything. How do I execute it?

My first idea was:

function runCommand(theOtherCommand) -- yes, it was received as an argument from another function
theOtherCommand
end

That didn't work though, as it gave me an error on end that said it expected an identifier or something, so that got ruled out. What way is there, if it exists?

1 answer

✓ Accepted answer

The problem here is, you can't anything other than a function, string, int, userdata, table or bool in a variable, meaning you can't put other code.

Here are my solutions:

Solution 1: Loadstring

This will only work if loadstring is enabled.

local commandToRun = "part1:Destroy()" -- I'm not entirely sure if you can use variables in loadstring though

function executeCommand(cmd)
	return loadstring(cmd)
end
executeCommand(commandToRun)

Solution 2: Function

function deletePart(part)
	part:Destroy()
end

deletePart(game.Workspace.Part)

Hope I helped!

~TDP

5 comments

0

That's not the issue. Let's say I didn't want to destroy it, and what I wanted to do with it was in the variable. How do I execute that?

0

What do you mean? Like using loadstring or something? loadstring("part:" ..command)?

0

Read the question now, I just edited it.

0

Finished edits.

0

Testing right now, but loadstring looks to be correct, thanks!

This discussion is preserved as it was. New answers, comments, and votes are closed.