So I need to program an NPC so that it'll detect the player if it spots suspicious activity. Here's the script I made:
local npc = script.Parent local iconGui = script.Parent.Head.aiGui local triggerPart = npc.DetectionPart.Handle local itemsInSight = {} local eis = false local curChar = nil local num = 0 local R = 0 local B = 200 local npcStatus = script.Parent["NPC Status"] local detected = false triggerPart.Touched:Connect(function(hit) print("New item-in-sight!") if game.Players:GetPlayerFromCharacter(hit.Parent) then if eis == false and detected == false then local plr = game.Players:GetPlayerFromCharacter(hit.Parent) local pGui = plr.PlayerGui local char = hit.Parent local detectGui = plr.PlayerGui.DetectGui curChar = char eis = true table.insert(itemsInSight, plr) script.Parent.Head.Eyes.Texture = game.ReplicatedStorage.Faces.SuspiciousEyes.Value local dFrame if script.DetectBar.Value == nil then local dFrame = plr.PlayerGui.DetectGui.Frame:Clone() script.DetectBar.Value = dFrame dFrame.Parent = plr.PlayerGui.DetectGui dFrame.Visible = true dFrame.Target.Value = script.Parent.HumanoidRootPart else dFrame = script.DetectBar.Value end --Scan for walls if table.getn(itemsInSight) >= 1 then repeat local InFrontOfWall = false local CheckNum = 0 repeat CheckNum = CheckNum +1 if itemsInSight[CheckNum] ~= nil then local Mag = itemsInSight[CheckNum] if Mag.ClassName == "Part" or Mag.ClassName == "MeshPart" or Mag.ClassName == "UnionOperation" then if (Mag.Position - script.Parent.Head.Position).Magnitude <= (hit.Position - script.Parent.Head.Position).Magnitude and hit.Name ~= "Handle" then InFrontOfWall = true else InFrontOfWall = false end end end wait() print("Check complete, CheckNum = "..CheckNum) until CheckNum >= table.getn(itemsInSight) or InFrontOfWall == true if CheckNum >= table.getn(itemsInSight) then num = num +1 R = R +2 B = B -2 dFrame:WaitForChild("Bar1")["Bar"..num].Visible = true dFrame.Bar2["Bar"..num].Visible = true local bars = dFrame.Bar1:GetChildren() for i = 1,#bars do bars[i].BackgroundColor3 = Color3.fromRGB(R, 0, B) end local bars = dFrame.Bar2:GetChildren() for i = 1,#bars do bars[i].BackgroundColor3 = Color3.fromRGB(R, 0, B) end elseif InFrontOfWall == true then eis = false end if eis == true then if (script.Parent.Head.Orientation - script.Parent.HumanoidRootPart.Orientation).Magnitude <= 90 and (script.Parent.Head.Orientation - script.Parent.HumanoidRootPart.Orientation).Magnitude >= -90 then script.Parent.Head.CFrame = CFrame.new(script.Parent.HeadCFramePart.Handle, hit.Parent.Head.Position) end if (hit.Position - script.Parent.Head.Position).Magnitude <= 50 and (hit.Position - script.Parent.Head.Position).Magnitude >= 40 then if plr.PlayerGui.MainStats.Status.Text == "Disguised" then wait(0.09) elseif plr.PlayerGui.MainStats.Status.Text == "Armed" or plr.PlayerGui.MainStats.Text == "Frightening" then wait(0.03) end elseif (hit.Position - script.Parent.Head.Position).Magnitude <= 40 and (hit.Position - script.Parent.Head.Position).Magnitude >= 30 then if plr.PlayerGui.MainStats.Status.Text == "Disguised" then wait(0.08) elseif plr.PlayerGui.MainStats.Status.Text == "Armed" or plr.PlayerGui.MainStats.Text == "Frightening" then wait(0.025) end elseif (hit.Position - script.Parent.Head.Position).Magnitude <= 30 and (hit.Position - script.Parent.Head.Position).Magnitude >= 20 then if plr.PlayerGui.MainStats.Status.Text == "Disguised" then wait(0.07) elseif plr.PlayerGui.MainStats.Status.Text == "Armed" or plr.PlayerGui.MainStats.Text == "Frightening" then wait(0.02) end elseif (hit.Position - script.Parent.Head.Position).Magnitude <= 20 and (hit.Position - script.Parent.Head.Position).Magnitude >= 10 then if plr.PlayerGui.MainStats.Status.Text == "Disguised" then wait(0.06) elseif plr.PlayerGui.MainStats.Status.Text == "Armed" or plr.PlayerGui.MainStats.Text == "Frightening" then wait(0.015) end elseif (hit.Position - script.Parent.Head.Position).Magnitude <= 10 and (hit.Position - script.Parent.Head.Position).Magnitude >= 0 then if plr.PlayerGui.MainStats.Status.Text == "Disguised" then wait(0.05) elseif plr.PlayerGui.MainStats.Status.Text == "Armed" or plr.PlayerGui.MainStats.Text == "Frightening" then wait(0.01) end end end wait() print("Proccess Complete") until num >= 100 or eis == false if eis == false then wait(3) print("Detect System Restart") repeat num = num -1 wait(0.01) until num <= 0 or eis == true if num <= 0 then script.Parent.DetectionManager.DetectBar.Value:Destroy() script.Parent.Head.Eyes.Texture = game.ReplicatedStorage.Faces.OpenEyes.Value script.Parent.Head.CFrame = CFrame.new(script.Parent.Head.Position, script.Parent.FaceFrontPart.Handle.Position) print("Detect System Restart Successful") end else print("Detected") script.Parent.HostageScript:Destroy() --'Ahhh! A criminal!!' local pGui = game.Players[hit.Parent.Name].PlayerGui if pGui.MainStats.Status.Text == "Armed" or pGui.MainStats.Status.Text == "Frightening" then workspace.Audio.NPCAlerted:Play() detected = true if pGui.MainStats.Status.Text == "Armed" then script.Parent.Head.aiGui.Alerted.Visible = true npcStatus.Value = "Alerted" local num = 0 repeat num = num +0.01 wait(0.01) until num >= 1 or pGui.MainStats.Status.Text == "Frightening" if num >= 1 then local num = 1 local gui = pGui.ReloadingTutorial gui.HintLabel.Text = "You took to long to frighten the civilian! Scope in before approaching her to make her surrender!" repeat num = num -0.05 gui.Image.ImageTransparency = num gui.HintLabel.TextTransparency = num gui.Label1.TextTransparency = num gui.Label2.TextTransparency = num wait(0.01) until num >= 1 wait(1) script.Parent.Head.aiGui.Alerted.Visible = false npcStatus.Value = "Neutral" hit.Parent.Parent.Head.CFrame = CFrame.new(game.Workspace.ResetTP2.Position) wait(1) repeat num = num +0.05 gui.Image.ImageTransparency = num gui.HintLabel.TextTransparency = num gui.Label1.TextTransparency = num gui.Label2.TextTransparency = num wait(0.01) until num <= 0 else npcStatus.Value = "Hostage" script.Parent.Humanoid:LoadAnimation(game.ReplicatedStorage.Animations["Hands Up"]):Play() script.Parent.Head.aiGui.Alerted.Visible = false wait(1) script.Parent.Humanoid:LoadAnimation(game.ReplicatedStorage.Animations["Hands Up Idle"]):Play() script.Parent.Head.Mouth.Texture = game.ReplicatedStorage.Faces.Frown.Value end elseif pGui.MainStats.Status.Text == "Frightening" then npcStatus.Value = "Hostage" script.Parent.Humanoid:LoadAnimation(game.ReplicatedStorage.Animations["Hands Up"]):Play() script.Parent.Head.aiGui.Alerted.Visible = false wait(1) script.Parent.Humanoid:LoadAnimation(game.ReplicatedStorage.Animations["Hands Up Idle"]):Play() npcStatus.Value = "Hostage" script.Parent.Head.Mouth.Texture = game.ReplicatedStorage.Faces.Frown.Value end if script.Parent.DetectionManager.DetectBar.Value ~= nil then script.Parent.DetectionManager.DetectBar.Value:Destroy() end end end end end else table.insert(itemsInSight, hit) end end) triggerPart.TouchEnded:Connect(function(hit) local plr = game.Players:GetPlayerFromCharacter(hit.Parent) if plr then table.remove(itemsInSight, table.find(itemsInSight, plr)) if eis == true then eis = false end else table.remove(itemsInSight, table.find(itemsInSight, hit)) end end)
I really have to solve this for the game (actively working on tutorial) and help would be appreciated. Thanks!
RAID (friends access): roblox.com/games/6034647915/RAID