Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Linking a table to an object in workspace?

If I have multiple tables like this:

local Bob 		= {
	Name		= "Bob",
	Character	= game.Workspace.NPCs["Bob"]
	Text			= "My name is Bob and I like to drink Caprison."
}
local Dummy 		= {
	Name		= "Dummy",
	Character	= game.Workspace.NPCs["Dummy"]
	Text			= "I am Dummy, hit me."
}

I also had a detection script further down:

Player.Character.HumanoidRootPart.TouchEnded:Connect(function(RangeBlock)
	NPC = RangeBlock.Parent
end)

The characters are all in a folder in the workspace.

How would I link the character to the table if the NPC's name is the same as the name of a variable?

For example, player touches NPC named Bob. How would I then print the Text of Bob and not of Dummy?

1 comment

0

if statement I would say?

1 answer

✓ 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)
    --get the actual NPC's character model
    local character = part.Parent;
    for i = 1, #NPC_Data do --loop through all of the NPC data
        if NPC_Data[i].Character == character then --if the current character in the iteration is equal to the character that the touch ended for
            print(NPC_Data[i].Name);
            --do stuff with bob
        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; --the text StringValue i was talking about before; assuming that its name is Text
    };
end

plr.Character.HumanoidRootPart.TouchEnded:connect(function(part)
    --get the actual NPC's character model
    local character = part.Parent;
    for i = 1, #NPC_Data do --loop through all of the NPC data
        if NPC_Data[i].Character == character then --if the current character in the iteration is equal to the character that the touch ended for
            print(NPC_Data[i].Name);
            --do stuff with bob
        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.

This discussion is preserved as it was. New answers, comments, and votes are closed.