Hello guys. I’m making inventory system, where I can save/load tools, which have Tip, Handle, and Acessories.
While connecting Tip with Handle isn’t big problem, but Accessories are.
They can be connected either to Handle or Tip, so, I decided to add Tag which will describe to which part they should be connected. Like “Tip1” means Acessory should be connected to Tip’s attachment with name “AcessoryAtt_1”
But, while implementing such system, I got question: is it possible to use already existing variable if I know it’s name, like:
--NOT this:
local Handle = SomePart
local Tip = AnotherPart
local AcessoryInfo = "Tip1"
local ConnectAtt = (if string.match(AcessoryInfo, "^%d+") == "Tip" then Tip else Handle)["AccessoryAtt_" .. string.match(AcessoryInfo, "%d+")]
--What I want (this statement will error, but I think you will understand what I want):
local ConnectAtt = script.[string.match(AcessoryInfo, "^%d+")]["AccessoryAtt_" .. string.match(AcessoryInfo, "%d+")]
So, in short, I want get rid of if-then-else statement, and get objects directly. Is it possible with Roblox?
local Handle = SomePart
local Tip = AnotherPart
local Accessories = {Tip = Tip, Handle = Handle}
local AcessoryInfo = "Tip1"
local partType, attachmentNum = string.match(AcessoryInfo, "(%a+)(%d+)")
local ConnectAtt = Accessories[partType]["AccessoryAtt_" .. attachmentNum]
local Handle = SomePart
local Tip = AnotherPart
local AcessoryInfo = "Tip1"
local partType, attachmentNum = string.match(AcessoryInfo, "(%a+)(%d+)")
local ConnectAtt = (partType == "Tip" and Tip or Handle)["AccessoryAtt_" .. attachmentNum]