Changing a string to an object value

How would I approach this? I have this code snippet:

if NewValue ~= nil then
	Value.Value = NewValue
end
if NewName ~= nil then
	Value.Name = NewName
end

but it just returns an error. I’d like to know how I can convert Value (in this case, a string equaling "TestStr" into an object value, so I can reference it.

They aren’t interchangeable datatypes, so you can’t do this. A StringValue holds a literal string in its value and an ObjectValue holds a reference to an object in its value.

Is there a specific reason why you are looking to do this? What’s your use case, or rather the scenario behind what you’re trying to achieve? You mentioned it earlier but a bit more detail would be appreciated. Are you not able to create a table with types? e.g.

-- format:
local stat = {statName = "Kills", statType = "Int"}

-- creation:
local class = ("%sValue"):format(stat.statType)
local object = Instance.new(class)
2 Likes

I figured out an alternative. I just used FindFirstChild instead of referencing it directly. Sorry if I was being too unclear. EDIT: For anyone else wondering, you can also put the string value in square brackets as you would with a name with spaces, and that will reference it too.