I need help with this script, basically what this script does is when I approach an NPC speficially named "Bob" and I am 5 studs away from him on keypress the dialog gui pops up this all works absolutely fine. But I would like to know how to add more NPCs to this without spamming this,
if (game.Players.LocalPlayer.Character.Torso.Position - Workspace.Bob.Torso.Position).magnitude <= distance then
elseif (game.Players.LocalPlayer.Character.Torso.Position - Workspace.Jim.Torso.Position).magnitude <= distance then
but instead I wish to put in a table to affect the listed npcs. NPC = {"Bob","Jim"} and also use the table, to find which NPC dialog to open. JimChat GUI or BobChat GUI
For example When the player is 5 or less studs away from Bob, I press E Bobs chat will appear.
My hierarchy. StarterGUI> -LocalScript
Workspace> -NPCs <-- with multiple children with different names i.e. Bob and Jim. --Bob --Jim
ReplicatedStorage> -BobChat -JimChat
How would I go about doing this? script below. Sorry if this really confusing, please help! much appreicated!
mouse = game.Players.LocalPlayer:GetMouse()
distance = 5 -- stud radius from object
Player = script.Parent.Parent
mouse.KeyDown:connect(function(key)
local box = "BobChat"
local gui = Player:WaitForChild("PlayerGui"):FindFirstChild(box)
if (game.Players.LocalPlayer.Character.Torso.Position - Workspace.Bob.Torso.Position).magnitude <= distance then
lol = "e"
if (key:lower()) == lol then
if Player:WaitForChild("PlayerGui"):FindFirstChild(box) then
Player:WaitForChild("PlayerGui"):FindFirstChild(box):Destroy()
else
game.ReplicatedStorage:FindFirstChild(box):Clone().Parent = Player.PlayerGui
end
end
end
end)

4 comments
I am getting "21:22:53.570 - Players.Player1.PlayerGui.KeyDialog:6: '}' expected (to close '{' at line 1) near '['" what does that mean?
I forgot a comma on line 5, should be better now
Don't worry I fixed it, added a comma after the } on line 5, and FindFirstChild("PlayerGui") and end). Many thanks for your help! much appreciated ^^
No problem!