I am trying to create a script that puts the letter E on NPCs. My code works for the first NPC that spawns but it when the second one spawns the letter E doesn’t show up. Can anyone let me know how to run a loop on this? I’ve tried but it just glitches the code out.
local RunService = game:GetService("RunService")
local UIS = game:GetService("UserInputService")
local Players = game:GetService("Players")
local MC = game.ReplicatedStorage.Events.MultipleCustomer
local IR = workspace.GameData.InRound
--Main
local DetectedNPC = nil
local Detected = false
local Chatting = false
local Initial = false
local Player = Players.LocalPlayer
local Camera = game.Workspace.CurrentCamera
local Gui = script.Parent
local Sounds = Gui.Sounds
local PromptLabel = Gui.PromptLabel
local Character = Player.Character or Player.CharacterAdded:Wait()
local CharHMR = Character:WaitForChild("HumanoidRootPart")
--NPC
local NPCS = game.Workspace:WaitForChild("NPCS")
--functions
UIS.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.E then
if Detected == true then
local order = DetectedNPC:FindFirstChild("Order")
if order then
Sounds.Click:Play()
Chatting = true
Detected = false
PromptLabel:TweenSize(UDim2.new(0,0,0,0), "Out", "Linear",0.2)
wait(.5)
order.Image.Visible = true
PromptLabel:TweenSize(UDim2.new(0,0,0,0), "Out", "Linear",0.2)
wait(.5)
Chatting = false
Detected = false
Initial = true
end
end
end
end)
--Main Loop
RunService.RenderStepped:Connect(function()
if Detected == false and Chatting == false and Initial == false then
for i, NPC in pairs(NPCS:GetChildren()) do
local Humanoid = NPC:FindFirstChild("Humanoid")
local HMR = NPC:FindFirstChild("HumanoidRootPart")
if Humanoid and HMR then
if (HMR.Position - CharHMR.Position).magnitude < 15 then
Detected = true
DetectedNPC = NPC
PromptLabel:TweenSize(UDim2.new(0,60,0,60), "In", "Linear")
print(DetectedNPC.Name)
end
end
end
end
if Detected == true and Chatting == false and Initial == false then
local Humanoid = DetectedNPC:FindFirstChild("Humanoid")
local HMR = DetectedNPC:FindFirstChild("HumanoidRootPart")
if Humanoid and HMR then
if (HMR.Position-CharHMR.Position).magnitude > 15 then
Detected = false
DetectedNPC = nil
PromptLabel:TweenSize(UDim2.new(0,0,0,0), "Out", "Linear")
print("No longer detected NPC")
else
local WTSP = Camera:WorldToScreenPoint(HMR.Position)
PromptLabel.Position = UDim2.new(0, WTSP.X, 0 ,WTSP.Y)
end
end
end
end)