So I’m working on a NPC interaction and I’ll have multiple NPCs in my game. Currently, my code will only check the distance from one NPC. (because I have it in a never ending while loop)
Any thoughts on how I would accomplish checking the distance from all the NPCs at once?
A friend suggested using Coroutines, but I’ve never used those and am not exactly sure if that’d be my best strategy.
Any help would be much appreciated!
My current Code:
(ModuleScript called locally in PlayerScripts)
local Module = {}
local Player = game.Players.LocalPlayer
local PlayerGui = Player.PlayerGui
local Distance = 12
function Module.CheckQuest(Level)
for _, Quest in pairs(game.Workspace[Level]:WaitForChild("Quests"):GetChildren()) do
local Collider = Quest:WaitForChild("NPC")
end
while wait(.2) do
if Player:DistanceFromCharacter(Collider.PrimaryPart.Position) <= Distance then
print("Close 'nuff")
end
if Player:DistanceFromCharacter(Collider.PrimaryPart.Position) > Distance then
print("Too Far Away!")
end
end
end
return Module