How to concatenate strings in order to refer to a variable?

  1. What do you want to achieve?
    I was wondering if I am able to concatenate a couple of strings to refer to an available variable.

  2. What is the issue?
    I’m not so sure if this method is possible. I have referred to this solution How To Concatenate Strings into a Variable - #13 by IProgramForFun but this solution talked about how to find the object names under other objects.

  3. What solutions have you tried so far?
    I have tried to use a similar method in the solution posted above, but I wasn’t able to concatenate without errors.

-- This is a sample dialog structure
local DialogNode = require(ServerScriptService.DialogNode)

local dialog0_1 = DialogNode.new({
	"NPC name: Sample text here"
}, {
	"Choice 1 state 0", 
	"Choice 2 state 0", 
	"Exit"
})

local dialog1_1 = DialogNode.new({
	"NPC name: Sample text here"
}, {
	"Choice 1 state 1", 
	"Choice 2 state 1", 
	"Exit"
})

local newDialog = Dialog.new(script.Parent.Name, script.Parent)

script.Parent.Head.ProximityPrompt.TriggerEnded:Connect(function(player)
	local state = ["dialog" .. newDialog.State .. "_1"] -- this is what I was attempting to do
	newDialog.CurrentDialog = state

-- This is how it suppose to look like:
local state = dialog0_1 -- if state of NPC is 0
local state = dialog1_1 -- if state of NPC is 1
		
	newDialog:Interact(player) -- function from module script
end)

Ya you could add them to a table or an array and pull your stings from it. However … that is really only “needed” when your list of possible choices are large, could be large or change. For what you’re doing here the way you got that set up is perfect. No need to over code a few choices.

1 Like

If I’m reading your code correctly, it should work as this works:

local a = "Bas"..game.Workspace.Baseplate.Transparency.."plate"
print(a) --prints "Bas0plate"

What exactly is newDialog.State?

The state at which the NPC is at. So for example, if you haven’t finished the quest that the NPC gave, it would be at state 0, giving state 0 dialogs. If you finish the quest, it would be state 1, giving state 1 dialogs.

I know that it’s not required for a small number of choices, but in case there are some huge dialogs that I might be making in the future, I would like to build a habit of making my code short without checking each dialog unnecessarily. I will try your table method though and see if it works :+1:

Will something like this be efficient?

local firstDialog = {}

table.insert(firstDialog, dialog0_1)
table.insert(firstDialog, dialog1_1)

local newDialog = Dialog.new(script.Parent.Name, script.Parent, graph)

script.Parent.Head.ProximityPrompt.TriggerEnded:Connect(function(player)
	newDialog.CurrentDialog = firstDialog[newDialog.State + 1] -- I put state at 0 by default
	print(newDialog.CurrentDialog.Value)
end)

You can use global variables and the function environment that represents the global environment of the script to achieve this.

A_Variable_Test = "Hello world!"

local function GetVarValFromStrs(...)
	local Args = {...}
	local Name = table.concat(Args, "_")
	local Val = getfenv(0)[Name]
	return Val
end

local Val = GetVarValFromStrs("A", "Variable", "Test")
print(Val) --Hello world!
2 Likes