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:
001 | local npc = script.Parent |
002 | local iconGui = script.Parent.Head.aiGui |
003 | local triggerPart = npc.DetectionPart.Handle |
004 | local itemsInSight = { } |
005 | local eis = false |
006 | local curChar = nil |
007 | local num = 0 |
008 | local R = 0 |
009 | local B = 200 |
010 | local npcStatus = script.Parent [ "NPC Status" ] |
011 | local detected = false |
012 |
013 | triggerPart.Touched:Connect( function (hit) |
014 | print ( "New item-in-sight!" ) |
015 | if game.Players:GetPlayerFromCharacter(hit.Parent) then |
016 | if eis = = false and detected = = false then |
017 | local plr = game.Players:GetPlayerFromCharacter(hit.Parent) |
018 | local pGui = plr.PlayerGui |
019 | local char = hit.Parent |
020 | local detectGui = plr.PlayerGui.DetectGui |
021 | curChar = char |
022 | eis = true |
023 | table.insert(itemsInSight, plr) |
024 | script.Parent.Head.Eyes.Texture = game.ReplicatedStorage.Faces.SuspiciousEyes.Value |
025 | local dFrame |
026 | if script.DetectBar.Value = = nil then |
027 | local dFrame = plr.PlayerGui.DetectGui.Frame:Clone() |
028 | script.DetectBar.Value = dFrame |
029 | dFrame.Parent = plr.PlayerGui.DetectGui |
030 | dFrame.Visible = true |
031 | dFrame.Target.Value = script.Parent.HumanoidRootPart |
032 | else |
033 | dFrame = script.DetectBar.Value |
034 | end |
035 |
036 | --Scan for walls |
037 | if table.getn(itemsInSight) > = 1 then |
038 | repeat |
039 | local InFrontOfWall = false |
040 | local CheckNum = 0 |
041 | repeat |
042 | CheckNum = CheckNum + 1 |
043 | if itemsInSight [ CheckNum ] ~ = nil then |
044 | local Mag = itemsInSight [ CheckNum ] |
045 | if Mag.ClassName = = "Part" or Mag.ClassName = = "MeshPart" or Mag.ClassName = = "UnionOperation" then |
046 | if (Mag.Position - script.Parent.Head.Position).Magnitude < = (hit.Position - script.Parent.Head.Position).Magnitude and hit.Name ~ = "Handle" then |
047 | InFrontOfWall = true |
048 | else |
049 | InFrontOfWall = false |
050 | end |
051 | end |
052 | end |
053 | wait() |
054 | print ( "Check complete, CheckNum = " ..CheckNum) |
055 | until CheckNum > = table.getn(itemsInSight) or InFrontOfWall = = true |
056 | if CheckNum > = table.getn(itemsInSight) then |
057 | num = num + 1 |
058 | R = R + 2 |
059 | B = B - 2 |
060 | dFrame:WaitForChild( "Bar1" ) [ "Bar" ..num ] .Visible = true |
061 | dFrame.Bar 2 [ "Bar" ..num ] .Visible = true |
062 | local bars = dFrame.Bar 1 :GetChildren() |
063 | for i = 1 ,#bars do |
064 | bars [ i ] .BackgroundColor 3 = Color 3. fromRGB(R, 0 , B) |
065 | end |
066 | local bars = dFrame.Bar 2 :GetChildren() |
067 | for i = 1 ,#bars do |
068 | bars [ i ] .BackgroundColor 3 = Color 3. fromRGB(R, 0 , B) |
069 | end |
070 | elseif InFrontOfWall = = true then |
071 | eis = false |
072 | end |
073 | if eis = = true then |
074 | if (script.Parent.Head.Orientation - script.Parent.HumanoidRootPart.Orientation).Magnitude < = 90 and (script.Parent.Head.Orientation - script.Parent.HumanoidRootPart.Orientation).Magnitude > = - 90 then |
075 | script.Parent.Head.CFrame = CFrame.new(script.Parent.HeadCFramePart.Handle, hit.Parent.Head.Position) |
076 | end |
077 | if (hit.Position - script.Parent.Head.Position).Magnitude < = 50 and (hit.Position - script.Parent.Head.Position).Magnitude > = 40 then |
078 | if plr.PlayerGui.MainStats.Status.Text = = "Disguised" then |
079 | wait( 0.09 ) |
080 | elseif plr.PlayerGui.MainStats.Status.Text = = "Armed" or plr.PlayerGui.MainStats.Text = = "Frightening" then |
081 | wait( 0.03 ) |
082 | end |
083 | elseif (hit.Position - script.Parent.Head.Position).Magnitude < = 40 and (hit.Position - script.Parent.Head.Position).Magnitude > = 30 then |
084 | if plr.PlayerGui.MainStats.Status.Text = = "Disguised" then |
085 | wait( 0.08 ) |
086 | elseif plr.PlayerGui.MainStats.Status.Text = = "Armed" or plr.PlayerGui.MainStats.Text = = "Frightening" then |
087 | wait( 0.025 ) |
088 | end |
089 | elseif (hit.Position - script.Parent.Head.Position).Magnitude < = 30 and (hit.Position - script.Parent.Head.Position).Magnitude > = 20 then |
090 | if plr.PlayerGui.MainStats.Status.Text = = "Disguised" then |
091 | wait( 0.07 ) |
092 | elseif plr.PlayerGui.MainStats.Status.Text = = "Armed" or plr.PlayerGui.MainStats.Text = = "Frightening" then |
093 | wait( 0.02 ) |
094 | end |
095 | elseif (hit.Position - script.Parent.Head.Position).Magnitude < = 20 and (hit.Position - script.Parent.Head.Position).Magnitude > = 10 then |
096 | if plr.PlayerGui.MainStats.Status.Text = = "Disguised" then |
097 | wait( 0.06 ) |
098 | elseif plr.PlayerGui.MainStats.Status.Text = = "Armed" or plr.PlayerGui.MainStats.Text = = "Frightening" then |
099 | wait( 0.015 ) |
100 | end |
101 | elseif (hit.Position - script.Parent.Head.Position).Magnitude < = 10 and (hit.Position - script.Parent.Head.Position).Magnitude > = 0 then |
102 | if plr.PlayerGui.MainStats.Status.Text = = "Disguised" then |
103 | wait( 0.05 ) |
104 | elseif plr.PlayerGui.MainStats.Status.Text = = "Armed" or plr.PlayerGui.MainStats.Text = = "Frightening" then |
105 | wait( 0.01 ) |
106 | end |
107 | end |
108 | end |
109 | wait() |
110 | print ( "Proccess Complete" ) |
111 | until num > = 100 or eis = = false |
112 | if eis = = false then |
113 | wait( 3 ) |
114 | print ( "Detect System Restart" ) |
115 | repeat |
116 | num = num - 1 |
117 | wait( 0.01 ) |
118 | until num < = 0 or eis = = true |
119 | if num < = 0 then |
120 | script.Parent.DetectionManager.DetectBar.Value:Destroy() |
121 | script.Parent.Head.Eyes.Texture = game.ReplicatedStorage.Faces.OpenEyes.Value |
122 | script.Parent.Head.CFrame = CFrame.new(script.Parent.Head.Position, script.Parent.FaceFrontPart.Handle.Position) |
123 | print ( "Detect System Restart Successful" ) |
124 | end |
125 | else |
126 | print ( "Detected" ) |
127 | script.Parent.HostageScript:Destroy() |
128 | --'Ahhh! A criminal!!' |
129 | local pGui = game.Players [ hit.Parent.Name ] .PlayerGui |
130 | if pGui.MainStats.Status.Text = = "Armed" or pGui.MainStats.Status.Text = = "Frightening" then |
131 | workspace.Audio.NPCAlerted:Play() |
132 | detected = true |
133 | if pGui.MainStats.Status.Text = = "Armed" then |
134 | script.Parent.Head.aiGui.Alerted.Visible = true |
135 | npcStatus.Value = "Alerted" |
136 | local num = 0 |
137 | repeat |
138 | num = num + 0.01 |
139 | wait( 0.01 ) |
140 | until num > = 1 or pGui.MainStats.Status.Text = = "Frightening" |
141 | if num > = 1 then |
142 | local num = 1 |
143 | local gui = pGui.ReloadingTutorial |
144 | gui.HintLabel.Text = "You took to long to frighten the civilian! Scope in before approaching her to make her surrender!" |
145 | repeat |
146 | num = num - 0.05 |
147 | gui.Image.ImageTransparency = num |
148 | gui.HintLabel.TextTransparency = num |
149 | gui.Label 1. TextTransparency = num |
150 | gui.Label 2. TextTransparency = num |
151 | wait( 0.01 ) |
152 | until num > = 1 |
153 | wait( 1 ) |
154 | script.Parent.Head.aiGui.Alerted.Visible = false |
155 | npcStatus.Value = "Neutral" |
156 | hit.Parent.Parent.Head.CFrame = CFrame.new(game.Workspace.ResetTP 2. Position) |
157 | wait( 1 ) |
158 | repeat |
159 | num = num + 0.05 |
160 | gui.Image.ImageTransparency = num |
161 | gui.HintLabel.TextTransparency = num |
162 | gui.Label 1. TextTransparency = num |
163 | gui.Label 2. TextTransparency = num |
164 | wait( 0.01 ) |
165 | until num < = 0 |
166 | else |
167 | npcStatus.Value = "Hostage" |
168 | script.Parent.Humanoid:LoadAnimation(game.ReplicatedStorage.Animations [ "Hands Up" ] ):Play() |
169 | script.Parent.Head.aiGui.Alerted.Visible = false |
170 | wait( 1 ) |
171 | script.Parent.Humanoid:LoadAnimation(game.ReplicatedStorage.Animations [ "Hands Up Idle" ] ):Play() |
172 | script.Parent.Head.Mouth.Texture = game.ReplicatedStorage.Faces.Frown.Value |
173 | end |
174 | elseif pGui.MainStats.Status.Text = = "Frightening" then |
175 | npcStatus.Value = "Hostage" |
176 | script.Parent.Humanoid:LoadAnimation(game.ReplicatedStorage.Animations [ "Hands Up" ] ):Play() |
177 | script.Parent.Head.aiGui.Alerted.Visible = false |
178 | wait( 1 ) |
179 | script.Parent.Humanoid:LoadAnimation(game.ReplicatedStorage.Animations [ "Hands Up Idle" ] ):Play() |
180 | npcStatus.Value = "Hostage" |
181 | script.Parent.Head.Mouth.Texture = game.ReplicatedStorage.Faces.Frown.Value |
182 | end |
183 | if script.Parent.DetectionManager.DetectBar.Value ~ = nil then |
184 | script.Parent.DetectionManager.DetectBar.Value:Destroy() |
185 | end |
186 | end |
187 | end |
188 | end |
189 | end |
190 | else |
191 | table.insert(itemsInSight, hit) |
192 | end |
193 | end ) |
194 |
195 | triggerPart.TouchEnded:Connect( function (hit) |
196 | local plr = game.Players:GetPlayerFromCharacter(hit.Parent) |
197 | if plr then |
198 | table.remove(itemsInSight, table.find(itemsInSight, plr)) |
199 | if eis = = true then |
200 | eis = false |
201 | end |
202 | else |
203 | table.remove(itemsInSight, table.find(itemsInSight, hit)) |
204 | end |
205 | 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