How do I make variables work with strings? (using script.Source)

I’m making a plugin and I want to put a command line (using script.Source) with variables and I can’t seem to find a solution

Here’s an example script:

local instance = workspace.Part
local testScript = workspace.TestScript
testScript.Source = 'instance.Name = "Test"'

Wanted result (inside the script):

workspace.Part.Name = "Test"

Gotten result (inside script):

instance.Name = "Test"

Any help would be appreciated!

workspace[instance.Name].Name = "Test"

I’m a bit confused, is this what you mean?

This is either complicated or impossible. I believe you should instead sandbox Luau, setting the desired environment.

You could use a mix of the string libary and :GetFullName() for example:

local instance = workspace.Part
local testScript = workspace.TestScript
testScript.Source = string.format('%s.Name = "Test"', instance:GetFullName())

EDIT: My code had bugs but now it doesnt

1 Like

that would work for this scenario however if im doing something like

local instance = workspace.Part
local position = Vector3.new(15,15,15)
local testScript = workspace.TestScript
testScript.Source = 'instance.Position = position'

it wouldnt work (obviously)

well, i have no idea how to do it then. Good luck.

I do have another idea, I could try turning the full variable value into a string but i’m not fully sure how to do so. I tried using something like tostring but that only returns a bit of the variable.

I did give you a method above using :GetFullName() which is how you get the full path of any instance.

It would work for the scenario I gave in the post however for other things like vector3, it wont work.

Try this:
testScript.Source = string.format('%s.%s = %s', instance:GetFullName(), --[[part propertie name]]"Name", --[[new value]] "Test")

You want something like this no?

local position = Vector3.new(15,15,15)
testScript.Source = string.format('%s.%s = %s', instance:GetFullName(), 
      --[[part propertie name]]"Position", 
      --[[new value]] position)

you just concatenate the string like any string

This would bring up the error of trying to concat a vector3 object and a string so you would just do a quick change such as:

local position = Vector3.new(15,15,15)
testScript.Source = string.format('%s.%s = Vector3.new(%s)', instance:GetFullName(), 
      --[[part propertie name]]"Position", 
      --[[new value]] tostring(position))

Unfortunately this means you have to change the string you are formatting for each object type. However you can detect the object type using typeof() which return the object name in the form of a string.

You will need to have a function to do that:

local function newToString(value)
	if typeof(value) == "Vector3" then
		return ("Vector3.new(%s, %s, %s)"):format(value.X, value.Y, value.Z) -- replace %s with respective value
	elseif typeof(value) == "CFrame" then -- another example
		return ("CFrame.new(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"):format(value:components())
	end
end

local position = Vector3.new(15,15,15)
testScript.Source = string.format('%s.%s = %s', instance:GetFullName(), 
	"Position",  -- part propertie name
	newToString(position)) -- "Vector3.new(15,15,15)"

-- Another exaple:
local cframe = CFrame.new(15,15,15)
testScript.Source = string.format('%s.%s = %s', instance:GetFullName(), 
	"CFrame",  -- part propertie name
	newToString(cframe)) -- "CFrame.new(15, 15, 15, 1, 0, 0, 0, 1, 0, 0, 0, 1)"

You can add more if you need it.

1 Like

Didn’t Ozzypig’s repr module explicitly deal with converting variables into reproducible string representations?

I think it converts all Roblox datatypes directly too.

repr is a debugging tool whose output looks distinctly code-like. It is not a data serialization tool. You should not be using it to store data or generate code, even though parts of its output may look appropriate for that.

(edit: although, I appreciate the shout-out, perhaps some part of repr can be used in OP’s case! didn’t want to sound unnecessarily hostile lol)

1 Like