Custom Dialogue: 'Attempt to concatenate a nil value' if dynamic choice and prompt function is running

:warning:Important: Dialogue is created with Roblox Dialogue Editor(Glossy ver.)

So I’ve created a dynamic choice and prompt basically what the NPC says that changes based on what the NPC’s initial prompt was. For example. If the NPC says ‘How are you?’ then the choice would be “I’m good”. However, if the NPC says something like 'Do you wanna go fishing with me? the choice would be ‘Sure’.

Ever since I’ve wrote this function and implemented it. I always get this error, which is.
[08:42:57.172 - ReplicatedStorage.RobloxDialogue.Class.Server:89: attempt to concatenate a nil value]

There are no errors in the function I wrote so far. There may be, however. If I remove the function the dialogue goes smoothly without the error coming up. However, dialogues are one of the crucial parts of my game. Without them, the player will not progress.

Here’s the snippet from the two functions that I’ve written that caused this error.

How my system works is that each NPC will have 5 folders of dialogues for each relationship level. Around 3-6 instances which are StringValues. A function will check the relationship level and if 1hour is up There is a one hour delay per potential friendship increasing text.. If conditions are met, then it will return the randomly selected text and by chance, will increase friendship level. Then, the two functions above will check the dialogue line to see if there will be a choice. If no, then dialogue ends once the player closes it.

dialogueResponse = function(player,dialogueFolder,node)
			if currentString == "Hey what's your name?" then --current string is the initial prompt that the NPC says that is set depending on the friendship level
				currentResponse = "My name's Min. How about you?"
				return currentResponse -- this is the part where 'currentResponse' will be the response text
				end
		end,
		
		dialoguePrompt = function(player,dialogueFolder,node)
			if currentResponse == "My name's Min. How about you?" then --current response is set by the 'dialogueResponse' function
				return "I'm Grizz. I remember you! I used to play with you when you were a child!" --returns NPC reply as text so that it can be displayed in the dialogue GUI.
			end
		end,

What code is on the given error line? Theres nothing in the code you provided that causes this.

It was like this. But if I edit it I’m afraid the whole dialogue system will break

function Server:FormatDynamicText(text, player, folder, node)
	repeat
		local open, close = text:find("<.->")
		if open and close then
			local funcName = text:sub(open + 1, close - 1)
			local funcs = require(script.Parent.Parent.DynamicTextFunctions)
			local func = funcs[funcName]
			if func then
				text = text:sub(1, open - 1)..func(player, folder, node)..text:sub(close + 1)
			else
				text = text:sub(1, open - 1).."ERROR: NO DYNAMIC TEXT FUNCTION "..funcName..text:sub(close + 1)
				warn(string.format("RobloxDialogue failed to find a dynamic text function called '%s.'", funcName))
			end
		end
	until (open == nil) and (close == nil)
	
	return text
end

Help us help you. If you show us which line is the error, it will save us a lot of time reading your code and guessing at which line caused it. Only you can see those line numbers in Studio to identify where the error is, unless you post a picture that contains the numbers (on the left hand side of the Script Editor window).

1 Like

I would assume it’s just a typo or something
dialogueResponse ONLY returns a string if the string given is exactly what it is looking for (which I don’t see defined)
dialoguePrompt does the same thing

Personally, instead of copy + pasting every text response and prompt, I would just put them in a chronological table for each NPC’s dialogue, and have the responses inside of that, so instead of passing the currentString and currentResponse to the function, you can simply check what the index given is instead of checking an entire string which could be subject to change.

As unrect clearly points out, your problem of the “attempt to concatenate a nil value” error, is due to the called func method from DynamicTextFunctions, does not explicitly return a value - so it implicitly returns nil.

And LUA does not allow to concatenate string-values with nil.

If you are too worried about changing too much in that FormatDynamicText method, you can avoid the error occurring - but not the actual problem - by slightly changing from:

if func then
    text = text:sub(1, open - 1)..func(player, folder, node)..text:sub(close + 1)
else

into:

if func then
    local msg = tostring( func(player, folder, node) )
    text = text:sub(1, open - 1) .. msg .. text:sub(close + 1)
else

The use of tostring( ) will convert whatever func returns, into a string-value. So nil will become a string-value "nil", that can be concatenated with other string-values.

Or you could change all methods in that DynamicTextFunctions, so they follow a strict pattern - ensuring that a string-value is explicitly returned, even when the if-then-end statement is not fulfilled:

function SomeMethodName()
    if <some expression> then
        return "some text"
    end
    return "some other text"
end
2 Likes

Apologies :sweat:. I’ll do that next time.

1 Like

Hm… What do you mean by “Not the actual problem”?

Did some tinkering and the error doesn’t show. Thanks

He means that you are not avoiding the actual problem–that is, your intended logic is not being run and you are defaulting to the generic, non-specific response given in your response functions. If you followed what Decker004 said to remove your errors, that means that your logic for the response that you intended to run is not running and will never run.

Ah. I see thats the case. Thanks for the explanation.