How to turn string into a Data type

I need to turn string into a Data Type

The only substitute I can think of is:

local value = "CFrame"
local datatype
if value == "CFrame" then
   datatype = CFrame
end
if value == "Color3" then
   datatype = Color3
end
if value == "Vector3" then
   datatype = Vector3
end 
--and so on

2 Likes

I think that u want to make it without copy and pasting if statements, you could use a dictionary and a for loop if you want to do it without too many if statements, then do this:

local value = "CFrame"
local types = {
	CFrame = "CFrame",
	Color3 = "Color3",
	Vector3 = "Vector3" --ect and end the last one without a coma
}
local datatype

for i, v in pairs(types) do
	if v == value then
		datatype = i
	end
end
1 Like

You can very easily just use the in-built typeof() function:

local variable = Vector3.new(0,0,0)

print(typeof(variable))
--Vector3
2 Likes

he said he wanted to turn a string, I dont know if that method would work with a string

2 Likes

type and typeof return strings

1 Like

oh yeah ur right I got confused

1 Like

These are the methods I was able to figure out:

Method 1(requires loadstring):

local function strToDatatype(str: string)
	local f = loadstring("return "..str)
	return f()
end

Method 2(requires hardcoding):

local datatypes = {Axes, BrickColor, CatalogSearchParams, CFrame, Color3, ColorSequence,
	ColorSequenceKeypoint, DateTime, DockWidgetPluginGuiInfo, Enum, Faces, FloatCurveKey, Font, 
	Instance, NumberRange, NumberSequence, NumberSequenceKeypoint, OverlapParams, PathWaypoint, 
	PhysicalProperties, Random, Ray, RaycastParams, Rect, Region3, Region3int16, SharedTable, 
	TweenInfo, UDim, UDim2, Vector2, Vector2int16, Vector3, Vector3int16}

local names = [[Axes, BrickColor, CatalogSearchParams, CFrame, Color3, ColorSequence,
ColorSequenceKeypoint, DateTime, DockWidgetPluginGuiInfo, Enum, Faces, FloatCurveKey, Font, 
Instance, NumberRange, NumberSequence, NumberSequenceKeypoint, OverlapParams, PathWaypoint, 
PhysicalProperties, Random, Ray, RaycastParams, Rect, Region3, Region3int16, SharedTable, 
TweenInfo, UDim, UDim2, Vector2, Vector2int16, Vector3, Vector3int16]]
names = names:gsub("\n", ""):gsub(" ", ""):split(",")

local function strToDatatype(str: string)
	local index = table.find(names, str)
	return datatypes[index]
end
2 Likes

oh wait ur wrong, sure typeof() returns strings, but I dont think it would return the string “CFrame” as CFrame. it would probably return as string

1 Like

typeof of datatypes returns "table" and tostring returns the table address, not the name. That’s why I had to hardcode both the datatypes and the names for my second solution.

1 Like

I was replying to the other guy that was replying to me replying to the other guy. they said that typeof() would return strings, and yeah they would, but I dont think they would return the string “CFrame” as CFrame, it would probably return as string. I wasnt talking about your method

1 Like

No, what I meant is that it won’t return “CFrame” either unless I’m missing something.

1 Like

haha u misread it

read it carefully, I said it would return as string

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.