I'm making a dialog system and I have 2 NPCs. It's a local script in StarterGui, and I can't seem to make it work for more than one NPC.
repeat wait(1) until game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
local NPC = game.Workspace.NPC -- folder
local UIS = game:GetService("UserInputService")
local radius = 7
local interact = script.Parent:WaitForChild("Interact")
local debounce = false
local dialog = game.ReplicatedStorage.Dialog
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local root = char:WaitForChild("HumanoidRootPart")
while wait(0.5) do
for i, v in pairs(NPC:GetChildren()) do
if (root.Position - v.HumanoidRootPart.Position).magnitude <= radius then
interact.TextLabel.Visible = true
UIS.InputBegan:Connect(function(key, gpe)
if gpe then return end
if key.KeyCode == Enum.KeyCode.B and not debounce and interact.TextLabel.Visible == true then
debounce = true
interact.TextLabel.Visible = false
game.ReplicatedStorage.Remotes.StartDialog:FireServer(v.NPCName.Value)
print(debounce)
wait(1)
debounce = false
end
end)
else
interact.TextLabel.Visible = false
end
end
end
I don't want to duplicate it in order to make it work.

3 comments
it still doesn't work but thanks for trying.
somehow i made it work. guess it was supposed to be elseif instead of else
good to hear!