✓ Accepted answer
I will take whack at this:
Inside the script, you can store the data for bob and dummy much more efficiently, which will enable you to get the data for bob or dummy accordingly:
local NPC_Data = {
{
Name = "Bob";
Character = game.Workspace.NPCs["Bob"];
Text = "My name is Bob and I like to drink Caprison.";
},
{
Name = "Dummy";
Character = game.Workspace.NPCs["Dummy"];
Text = "I am Dummy, hit me.";
}
};
plr.Character.HumanoidRootPart.TouchEnded:connect(function(part)
local character = part.Parent;
for i = 1, #NPC_Data do
if NPC_Data[i].Character == character then
print(NPC_Data[i].Name);
end
end
end)
But, you can be even more efficient by loading Data into NPC_Data by iterating through your NPCs folder, and if you insert a StringValue with a value equal to their text, than you can load the Text element based off of that unique StringValue inside of each NPC:
local NPC_Data = {};
for i,v in pairs(game.Workspace.NPCs:GetChildren()) do
NPC_Data[#NPC_Data + 1] = {
Name = v.Name;
Character = v;
Text = v.Text;
};
end
plr.Character.HumanoidRootPart.TouchEnded:connect(function(part)
local character = part.Parent;
for i = 1, #NPC_Data do
if NPC_Data[i].Character == character then
print(NPC_Data[i].Name);
end
end
end)
this way you do not have to waste your time writing out each and every name, character, and text for each NPC you have; imagine if you had 100 NPCs, simply loading in the NPC_Data would be much preferred instead of having to manually type it all in.
I hope this helped answer your question.
1 comment
if statement I would say?