How do I convert the result of a function into a string lua

Alright so I’m using load string to run custom commands. Now the problem is is that load string converts my command into a function. now what I want it to do is print the result of the command onto a surface gui except I don’t know how to convert the result of a function into a string. If anyone can help me that would be greatly appreciated. I’m not showing the code because its not really necessary to solve this.
But if you want the code I can send it to you Thanks.

You can do something like this:

local Surface =  workspace.Part.SurfaceGui

local function result(25, 25)
local result = 25 + 25
Surface.TextLabel.Text = result
end
result()
1 Like

That’s a good idea except load string doesn’t use any parameters ex func(A, B). And on top of that it load string docent show me the code of the function so I cant really call it. The function just prints something

What do you mean it just prints something? Have you looked at this part of the code?
Surface.TextLabel.Text = result

Oh, I’ll remove the parameters then.

You can do something like this:

local Surface =  workspace.Part.SurfaceGui

local function result()
local result = 25 + 25
Surface.TextLabel.Text = result
end
result()

You can return a function and just add additional functionality to that

local add = loadstring('return function(a, b) return tostring(a + b) end')()
print(add(2, 2)) --> Outputs: 4

no but with loadstring you cant view the function as far as im aware and since the player will
be typing custom commands i cant really get the code of the command. so i wont be able to know
what a and b even is plus with more simple commands like Print(“hello world”) there wont be any
parameters

I’m honestly a bit confused at what you’re trying to accomplish. Do you have a command that executes code? Like typing in chat !run print('Hello world') executes that code?

Yes thats exactly it except its on a computer so instead of you typing it on chat it would be on a surface gui

you could use a multi line string???

Alright so then:

local executeCode = loadstring('return function() ' .. SurfaceGui.Text .. ' end')()
print(executeCode())

So if the user types the following code onto the SurfaceGui:

print('Hello world!') --> Outputs: Hello world

or if they return something:

local a = 5
local b = 10
return a + b --> Outputs: 15
1 Like

ill try that thanks that i think that will do it

Basically, make a text box and make it appear when the player touches a part. Then:

if textbox.Text == "print" then
local a = 15
local b = 20
local result = a + b
print(result)
end

@AstrealDev’s idea is probably better then that

The code needs to be dynamic. You’re hard coding these values where as @blondeguy2001 wants to give users control of the code they write.

Can you explain this code a little bit I don’t really understand it

So loadstring() executes code. The way it works is it will return a function to execute that code:

loadstring("print('Hello world!')")() --> Outputs: Hello world!

By using the parentheses at the end you call the returned function used to execute the code

In the solution I provided to you I’m just wrapping the SurfaceGui text in another function that will be returned by the initial loadstring() function that is returned. It’s like doubling loadstring() basically. But this allows users to return a value since all you’re really doing is creating a function from a string.

Alright so i ran into another problem when i insert the string into the surface gui it errors
image

Yes because executeCode() is a function. You need to set the text to the value it returns I was printing it as an example… Don’t print it and simply call the function and set e.Text to the returned value.

e.Text = executeCode()