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

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.

That is beyond the point. I can safely say that, from a maintainability and security perspective, loadstring and its equivalents in any other language are not to be used under any circumstances in any environment with a non-developer end user and even then it’s not a good idea

Can you explain what this means? I don’t get it.

Let’s assume you’re trying to make a developer product. The only people touching the code will be developers, and likewise it can only affect the developer using it. Realistically, this would be something similar to a repl or some non-production precompiler / weird type checker that gets its types from runtime information. Maybe, in that environment, loadstring could be used. It still would not be advised, and is likely outside of the of any product deployed through Roblox.

In essence, “non-developer end user” means “at some point the code will affect someone who is not a developer”

That is false,

loadstring() will allow you to execute strings as if they were code.

Users can send strings from client to server through RemoteFunctions, if present.
They could then be executed through some function and modify structures like Datastores etc.

This is how loadstring() is used to exploit, whatever is contained in the string can run as code on the Server.

Please take a look into OOP. Not only will it help you with Lua code, it will also help you with many other languages. I became a much better programmer when I started listening to my professors about proper layout of code.

simply put, if you want to make a coffee with different flavors or what have you, you could do a module script with:

module script:

coffee = {};

-- + coffee.new(typeOfCoffee: string)
coffee.new = (function(typeOfCoffee)
	local self = {};
	self.typeOfCoffee = "";

	-- + coffe:getTypeOfCoffee(): string
	function self:getTypeOfCoffee()
		return self.typeOfCoffee
	end

	--constructor
	do
		self.typeOfCoffee = typeOfCoffee
	end

	return self;
end)

in another script:

local coffee = require(ModuleScript);

local myBrew = coffee.new("Espresso");

print(myBrew:getTypeOfCoffee()) -> Espresso

The sooner you get more comfortable with OOP, the easier you will be able to create complicated creations with multiple different types of classes.