Heya!
I’m working on a simple dialogue system for a game.
Looks like this at the moment of writing this post:
In the code for the system, I made it so every NPC has a module script inside of them that has a table with all the dialogue options, messages and functions to execute.
The whole code is a bit complex so I’ll avoid going into too much detail to keep this question simple.
Here’s what one of the module scripts would look like:
local Info = {}
Info.LeaveMessage = "Alright. Goodbye then!"
Info.Dialogue = {
[1] = {
Text = "Hey, I need help collecting some more crops, could you do me a favor and get me 25 Wheat?";
Choices = {
[1] = { Text = "Sure!"; GoTo = 2 };
[2] = { Text = "Sorry, I'm busy."; };
};
};
[2] = {
Text = "Thanks bud! Just collect some right here behind me!";
Choices = {
[1] = { Text = "Alright!"; GoTo = 3; Follow = true; };
[2] = { Text = "Let's do this!"; GoTo = 4; Follow = true; };
};
};
[3] = {
Text = "Alright! Good luck!";
Execute = function(Player)
if Player then
print(Player.Name.." is cool!")
else
print("Couldn't get Player, but they're still cool!")
end
end;
Choices = {};
};
[4] = {
Text = "I'll be here if you need me.";
Execute = function(Player)
if Player then
print(Player.Name.." is cool!")
else
print("Couldn't get Player, but they're still cool!")
end
end;
Choices = {};
};
}
return Info
The “Execute” you can see inside some of the Messages of the Dialogue table is a function that would get executed when that Message is displayed.
To display the message, I use a LocalScript, here’s what the relevant part to the question looks like:
if Dialogue.Execute then -- Check if there's an Execute function inside the current Message
Dialogue.Execute(Player) -- Execute the function
end
For some reason, although there are functions in the Messages, the if statement never returns true.
I tried printing the Info.Dialogue list before executing the if statement, and for some reason, it looks like this:
{
[1] = {
["Choices"] = {
[1] = {
["GoTo"] = 2,
["Text"] = "Sure!"
},
[2] = {
["Text"] = "Sorry, I'm busy."
}
},
["Text"] = "Hey, I need help collecting some more crops, could you do me a favor and get me 25 Wheat?"
},
[2] = {
["Choices"] = {
[1] = {
["Follow"] = true,
["GoTo"] = 3,
["Text"] = "Alright!"
},
[2] = {
["Follow"] = true,
["GoTo"] = 4,
["Text"] = "Let's do this!"
}
},
["Text"] = "Thanks bud! Just collect some right here behind me!"
},
[3] = {
["Choices"] = {},
["Text"] = "Alright! Good luck!"
},
[4] = {
["Choices"] = {},
["Text"] = "I'll be here if you need me."
}
}
The functions disappeared?
Weirdly enough, when I print the content of the same list but from inside the Module Script, it returns the list WITH the functions.
Also, although this weird error happens. [“Choices”] and [“Text”] are always returned correctly, so the Dialogue System still works (without executing the needed functions though…).
Is this the expected Luau behavior? If so what work-arounds should I use?
Otherwise, where did I mess up?
I’ve looked on the forums and documentations and got nowhere.
Any and all answers are very welcomed!
Thanks in advance Developers!