I’m trying to make an npc chat text strings I listed in a module to make a dialog
The strings in the tables are not strings but are somehow tables. I don’t see them as tables, but the script does. Its giving me the error Workspace.QuestSandBox.QuestScript:20: invalid argument #2 (string expected, got table).
Ive tried doing unpack(), but then, it tells me its a string. Im confused at this point.
QuestScript (the script that gets the module)
local QuestDetails = require(script.Parent.QuestDetails)
local QuestLine = QuestDetails.text.QuestLine
local QuestNPCs = QuestDetails.speakers.QuestNPCs
function Chat(head,msg,color)
game:GetService("Chat"):Chat(head, msg, color)
end
local dh = script.Parent.Dummy.Humanoid
function StartDiolog()
local hasFired = false
if hasFired then return end
hasFired = true
for i, val in pairs(QuestLine) do
wait(1)
print(val.speaker)
print(val.txt)
Chat(script.Parent[val.speaker], val.txt, Enum.ChatColor.White)
local sound = game.Workspace.QuestLineNotifications[QuestNPCs[val.speaker]]:Clone()
-- make sound parent speakers head
sound.Parent = script.Parent[val.speaker].Head
sound:Play()
end
end
script.Parent.PAD.Touched:Connect(function()
StartDiolog()
end)
QuestDetails (Module that gets required)
local module1 = {
QuestNPCs = {
["Dummy"] = {
DialogSound = "frostilism"
};
};
}
local module2 = {
QuestLine = {
txt1 = {
txt = "Hello world! This is a test message.";
speaker = module1.QuestNPCs.Dummy
};
txt2 = {
txt = "This works!";
speaker = module1.QuestNPCs.Dummy
};
txt3 = {
txt = "This works. Im happy to know! And im mr dummy man! See ya!";
speaker = module1.QuestNPCs.Dummy
};
}
};
local module = {
speakers = module1;
text = module2
}
return module
This could also be the case but from what I’m seeing, OP is trying to index script.Parent with val.Speaker (a table from module1). If they were to replace the second argument val.txt with just a raw string such as "test", it would make it clear if it is the index error or something else
this is happening becuase as @Dede_4242 said you are calling the whole table and this is why you got this error, to fix this you simply need to tell the script which variable you want from this table in our case its txt, also why u assigning the table to another variable when you can just use same table with different Variables in one table?
local NPc_Chat = {
["QuestNPCs"] = {
["Dummy"] = {
DialogSound = "frostilism"
};
};
["QuestLine"] = {
txt1 = {
txt = "Hello world! This is a test message.";
speaker = module1.QuestNPCs.Dummy
};
txt2 = {
txt = "This works!";
speaker = module1.QuestNPCs.Dummy
};
txt3 = {
txt = "This works. Im happy to know! And im mr dummy man! See ya!";
speaker = module1.QuestNPCs.Dummy
};
}
}
return NPc_Chat
in the Main Script:
local QuestLine = QuestDetails["QuestLine"]["txt1"].txt --"Hello world! This is a test message."
-- to get the speaker of txt1:
local QuestNPCs = QuestDetails["QuestLine"]["txt1"].speaker