Is there a way to turn a string into a reference?

For example, say I had a string in a variable like this:

local x = "game.Workspace.Part"

Would there be a way to, for lack of a better word, remove the quotation marks from this and use it to reference something?

I couldn’t find anything about this while searching

1 Like

Yes, you can split the string into words and then search for the object recursively.

function convert(path)
	local position = game
	
	local path = string.gmatch(path, '[%w%_]+');
	for v in path do
        if v ~= "game" then
		    position = position[v];
		    if position == nil then
		    	error('Invalid path');
		    end
        end
	end
	
	return position
end

local x = convert("game.Workspace.Part")

However this will only work for absolute paths, if you want relative paths to work then we would have to modify the code a bit.

5 Likes

I’m trying to store “Cup.Name” as a string value inside an array and use it as a reference later, the reason I’m trying to do this is because I need “Cup.Name” before Cup.Name can be defined.

Also, this is slightly of out of my skill level, so I would appreciate if you could guide me though it a bit.

2 Likes

I am sure that there’s no need to store anything like that in an array, can you describe why are you doing that and what is the whole use case?

You can always check if Cup is not nil and then reference Cup.Name.

1 Like
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")

local directory = workspace.CafeMachines

local authorizedItems = {
	--CUPS
	"Large Cone";
	"Medium Cone";
	"Small Cone";
	"Large Cup";
	"Medium Cup";
	"Small Cup";
	"Large Glass";
	"Medium Glass";
	"Small Glass";
	"Large Mug";
	"Medium Mug";
	"Small Mug";
	--COFFEE
	"Americano";
	"Cappuccino";
	"Decaf";
	"Espresso";
	"Frappe";
	"Latte";
	"Macchiato";
	"Mocha";
	"Regular";
	--ADDONS
	"Blender";
	"Cream";
	"MilkCarton";
	"Sugar";
	"Water";
}

local authorizedMachines = {
	"AmericanoMachine";
	"CappuccinoMachine";
	"DecafMachine";
	"EspressoMachine";
	"FrappeMachine";
	"LatteMachine";
	"MacchiatoMachine";
	"MochaMachine";
	"RegularMachine";
	"Water";
	"AssamTea";
	"GreenTea";
	"OolongTea";
	"WhiteTea";
	"ChocolateIceCream";
	"MintIceCream";
	"StrawberryIceCream";
	"VanillaIceCream";
	"MilkCarton";
	"BananaMilkshake";
	"BlueberryMilkshake";
	"LemonMilkshake";
	"LimeMilkshake";
	"OrangeMilkshake";
	"StrawberryMilkshake";
	"Blender";
	"Ice";
	"Sugar";
}

local machineRequirements = {
	["AmericanoMachine"] = {MachineName = "AmericanoMachine", acceptedItems = {"Small Cup", "Medium Cup", "Large Cup"}, product = "Americano"},
    ["CappuccinoMachine"] = {MachineName = "CappuccinoMachine", acceptedItems = {"Small Cup", "Medium Cup", "Large Cup"}, product = "Cappucino"},
    ["DecafMachine"] = {MachineName = "DecafMachine", acceptedItems = {"Small Cup", "Medium Cup", "Large Cup"}, product = "Decaf"},
    ["EspressoMachine"] = {MachineName = "EspressoMachine", acceptedItems = {"Small Cup", "Medium Cup", "Large Cup"}, product = "Espresso"},
    ["FrappeMachine"] = {MachineName = "FrappeMachine", acceptedItems = {"Small Cup", "Medium Cup", "Large Cup"}, product = "Frappe"},
    ["LatteMachine"] = {MachineName = "LatteMachine", acceptedItems = {"Espresso"}, product = "Latte"},
    ["MacchiatoMachine"] = {MachineName = "MacchiatoMachine", acceptedItems = {"Small Cup", "Medium Cup", "Large Cup"}, product = "Macchiato"},
    ["MochaMachine"] = {MachineName = "MochaMachine", acceptedItems = {"Small Cup", "Medium Cup", "Large Cup"}, product = "Mocha"},
    ["RegularMachine"] = {MachineName = "RegularMachine", acceptedItems = {"Small Cup", "Medium Cup", "Large Cup"}, product = "Regular"},
    ["Water"] = {MachineName = "Water", acceptedItems = {"Small Mug", "Medium Mug", "Large Mug"}, product = "Mug of Water"},
    ["AssamTea"] = {MachineName = "AssamTea", acceptedItems = {"Mug of Water"}, product = "Mug of Assam Tea"},
    ["GreenTea"] = {MachineName = "GreenTea", acceptedItems = {"Mug of Water"}, product = "Mug of Green Tea"},
    ["OolongTea"] = {MachineName = "OolongTea", acceptedItems = {"Mug of Water"}, product = "Mug of Oolong Tea"},
    ["WhiteTea"] = {MachineName = "WhiteTea", acceptedItems = {"Mug of Water"}, product = "Mug of White Tea"},
    ["ChocolateIceCream"] = {MachineName = "ChocolateIceCream", acceptedItems = {"Small Cone", "Medium Cone", "Large Cone"}, product = "Chocolate Ice Cream"},
    ["MintIceCream"] = {MachineName = "MintIceCream", acceptedItems = {"Small Cone", "Medium Cone", "Large Cone"}, product = "Mint Ice Cream"},
    ["StrawberryIceCream"] = {MachineName = "StrawberryIceCream", acceptedItems = {"Small Cone", "Medium Cone", "Large Cone"}, product = "Strawberry Ice Cream"},
    ["VanillaIceCream"] = {MachineName = "VanillaIceCream", acceptedItems = {"Small Cone", "Medium Cone", "Large Cone"}, product = "Vanilla Ice Cream"},
    ["MilkCarton"] = {MachineName = "MilkCarton", acceptedItems = {"Small Glass", "Medium Glass", "Large Glass"}, product = "Glass of Milk"},
    ["BananaMilkshake"] = {MachineName = "BananaMilkshake", acceptedItems = {"Glass of Milk"}, product = "Banana Milkshake"},
    ["BlueberryMilkshake"] = {MachineName = "BlueberryMilkshake", acceptedItems = {"Glass of Milk"}, product = "Blueberry Milkshake"},
    ["LemonMilkshake"] = {MachineName = "LemonMilkshake", acceptedItems = {"Glass of Milk"}, product = "Lemon Milkshake"},
    ["LimeMilkshake"] = {MachineName = "LimeMilkshake", acceptedItems = {"Glass of Milk"}, product = "Lime Milkshake"},
    ["OrangeMilkshake"] = {MachineName = "OrangeMilkshake", acceptedItems = {"Glass of Milk"}, product = "Orange Milkshake"},
    ["StrawberryMilkshake"] = {MachineName = "StrawberryMilkshake", acceptedItems = {"Glass of Milk"}, product = "Strawberry Milkshake"},
    ["Blender"] = {MachineName = "Blender", acceptedItems = {"Strawberry Milk", "Orange Milk", "Lemon Milk", "Lime Milk", "Blueberry Milk", "Banana Milk"}, product = "Strawberry Milkshake", "Orange Milkshake", "Lemon Milkshake", "Lime     Milkshake", "Blueberry Milkshake", "Banana Milkshake"},
    ["Ice"] = {MachineName = "Ice", acceptedItems = {"Americano", "Cappucino", "Decaf", "Espresso", "Frappe", "Latte", "Macchiato", "Mocha", "Regular", "Americano w/Cream", "Cappucino w/Cream,", "Decaf w/Cream", "Espresso w/Cream", "Frappe w/Cream", "Latte w/Cream", "Macchiato w/Cream", "Mocha w/Cream", "Regular w/Cream", "Americano w/Sugar", "Cappucino w/Sugar", "Decaf w/Sugar", "Espresso w/Sugar", "Frappe w/Sugar", "Latte w/Sugar", "Macchiato w/Sugar", "Mocha w/Sugar", "Regular w/Sugar", "Americano w/Sugar & Cream", "Cappucino w/Sugar & Cream", "Decaf w/Sugar & Cream", "Espresso w/Sugar & Cream", "Frappe w/Sugar & Cream", "Latte w/Sugar & Cream", "Macchiato w/Sugar & Cream", "Mocha w/Sugar & Cream", "Regular w/Sugar & Cream"}, product = "Cup.Name"},
    --["Cream"] = {MachineName = "Cream", acceptedItems = {"Americano", "Cappucino", "Decaf", "Espresso", "Frappe", "Latte", "Macchiato", "Mocha", "Regular", "Iced Americano, "Iced Cappucino", "Iced Decaf", "Iced Espresso", "Iced Frappe", "Iced Latte", "Iced Macchiato", "Iced Regular", "Americano w/ Sugar", "Cappucino w/Sugar", "Decaf w/Sugar", "Espresso w/Sugar", "Frappe w/Sugar", "Latte w/Sugar", "Macchiato w/Sugar", "Mocha w/Sugar", "Regular w/Sugar", "Iced Americano w/Sugar", "Iced Cappucino w/Sugar", "Iced Decaf w/Sugar", "Iced Espresso w/Sugar", "Iced Frappe w/Sugar", "Iced Latte w/ Sugar", "Iced Macchiato w/Sugar", " Iced Mocha w/Sugar", "Iced Regular w/Sugar"}, product = "Americano w/Cream", "Cappucino w/Cream", "Decaf w/Cream", "Espresso w/Cream", "Frappe w/ Cream", "Latte w/Cream", "Macchiato w/Cream", "Mocha w/Cream", "Regular w/Cream", "Iced Americano w/Cream", "Iced Cappucino w/Cream", "Iced Decaf w/Cream", "Iced Espresso w/Cream", "Iced Frappe w/Cream", "Iced Latte w/Cream", "Iced Macchiato w/Cream", "Iced Mocha w/Cream", "Iced Regular w/Cream", "Americano w/ Sugar & Cream", "Cappucino w/Sugar & Cream", "Decaf w/Sugar & Cream", "Espresso w/Sugar & Cream", "Frappe w/Sugar & Cream", "Latte w/Sugar & Cream", "Macchiato w/Sugar & Cream", "Mocha w/Sugar & Cream", "Regular w/Sugar & Cream", "Iced Americano w/Sugar & Cream", "Iced Cappucino w/Sugar & Cream", "Iced Decaf w/Sugar & Cream", "Iced Espresso w/Sugar & Cream", "Iced Frappe w/Sugar & Cream", "Iced Latte w/ Sugar & Cream", "Iced Macchiato w/Sugar & Cream", " Iced Mocha w/Sugar & Cream", "Iced Regular w/Sugar & Cream"},
    ["Sugar"] = {MachineName = "Sugar", acceptedItems = {"Americano", "Cappucino", "Decaf", "Espresso", "Frappe", "Latte", "Macchiato", "Mocha", "Regular", "Americano w/Cream", "Cappucino w/Cream,", "Decaf w/Cream", "Espresso w/Cream", "Frappe w/Cream", "Latte w/Cream", "Macchiato w/Cream", "Mocha w/Cream", "Regular w/Cream", "Iced Americano", "Iced Cappucino", "Iced Decaf", "Iced Espresso", "Iced Frappe", "Iced Latte", "Iced Macchiato", "Iced Mocha", "Iced Regular", "Iced Americano w/Cream", "Iced Cappucino w/Cream", "Iced Decaf w/Cream", "Iced Espresso w/Cream", "Iced Frappe w/Cream", "Iced Latte w/Cream", "Iced Macchiato w/Cream", "Iced Mocha w/Cream", "Iced Regular w/Cream"}, product = "cup.Name"},
}

for _,dispenser in pairs(directory:GetDescendants()) do
	if dispenser:IsA("Part") and dispenser.Name == "Dispenser" then
		dispenser.Touched:Connect(function(hit)
			for _,valid in pairs(authorizedItems) do
				if hit.Parent.Name == valid then
					local cup = hit.Parent
					for _,val in pairs(authorizedMachines) do
						if dispenser.Parent.Name == val then
							local AddOn = false
							local machine = dispenser.Parent
							local array = machineRequirements[machine.Name]
							if machine.AddOn then
								AddOn = true
							end
							for _,v in pairs(array.acceptedItems) do
								if cup.Name == v then
									cup.Name = array.product
								end
							end
						end
					end
				end
			end
		end)
	end
end

This is the code, I apologise for the table being incredibly messy. The last 2 lines have the “Cup.Name”.

I attempted using BoolValues with local AddOn, but it didn’t work, that’s why I’m resorting to this method.

1 Like

Can you explain what are you trying to achieve?

1 Like

I’m trying to create a system that allows users to use machines to create drinks such as milkshakes, teas, and coffees. If you’ve ever played any cafe game on Roblox you’ll know exactly how it works.

I used to have 400 lines of individual functions for each item and decided to use arrays and for loops to clean it up.

The reason for the massive array is for the many recipe combinations and to allow for easy customization.

Basically, when the tool (coffee cup, milkshake, etc) is renamed, some of the new names require concatenation and I wasn’t able to use BoolValues to determine what items need concatenation.

But cup.Name = cup.Name doesn’t change the name of a cup, is there going to be another situation when you rename it to something else? (not array.product and not cup.Name)

Can’t you just do workspace[“Part”]? This is more simple than anything else. I don’t tested, but you can try this:

local x = loadstring('return function() return workspace["Part"] end')()()

I don’t think that is the best thing but I think it works well.

1 Like

Enabling loadstring is not safe, there’s certainly a better way do to what the OP wants to achieve.

Sorry, the last for loop should look like this:

for _,v in pairs(array.acceptedItems) do
    if array.product == "w/Sugar" then
        local x = "w/Sugar"
        cup.Name = array.product..x
    else
        cup.Name = array.product
    end
end

As you can see, I run into a bottleneck here, I can’t combine Cup.Name while checking if

if array.product == "w/Sugar" then

is true or false

Also, .product was originally “w/Sugar”, but I was experimenting.

Loadstring isn’t deprecated and is still working good.

One idea that I have is to save an array of information: [workspace or other, folder.Name, child.Name, item.Name, etc.] About the item. You would then do

array[1]:WaitForChild(array[2]):WaitForChild(array[3]) and so on.

Not sure if this is the best way to do it, though.

1 Like

That’s what the main array already does, stores data.

This is overly complicated, you should be storing addons in an array, for example:

local product = {productName = "", Addons = {}}
-- and for example:
product.ProductName = "Blueberry Milkshake"
table.insert(product.Addons, "Sugar")
table.insert(product.Addons, "Ice")
-- and now print it like that:
function tostringProduct(p)
    local text = p.ProductName
    for _,v in ipairs(p.Addons) do
        text = text .. " w/" .. v
    end
    return text
end

print(tostringProduct(product)) -- Blueberry Milkshake w/Sugar w/Ice

I appreciate the response, but would there be a way to do this with my current system? I really don’t have the mental capacity to do this all over again.

It will be easier to modify than continue with the current system, because in the future if you would want to expand your system with new products or ingredients it will be a nightmare.

Alright, I’ll try it later, I have to go.

Regardless of whether it works or not, loadstring allows users to run arbitrary server code which means exploiters can easily modify your Datastores and run any code they want.

1 Like

You can’t use loadstring on client, it will eror. So there isn’t any way to run codes and destroy your game.