Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Is it possible to execute a variable?

Asked by 7 years ago
Edited 7 years ago

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

Log in to vote
1
Answered by 7 years ago
Edited 7 years ago

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

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? idkaname8 25 — 7y
0
What do you mean? Like using loadstring or something? loadstring("part:" ..command)? TheDeadlyPanther 2460 — 7y
0
Read the question now, I just edited it. idkaname8 25 — 7y
0
Finished edits. TheDeadlyPanther 2460 — 7y
0
Testing right now, but loadstring looks to be correct, thanks! idkaname8 25 — 7y
Ad

Answer this question