Nicosarea
|
  |
| Joined: 07 Dec 2007 |
| Total Posts: 4424 |
|
|
| 17 Dec 2016 04:08 PM |
Hello folks! I'm having a problem where I can't modify my turret script to detect NPCs in the game. It detects players perfectly, however it can't identify NPCs.
Can anyone help me out?
--START OF SCRIPT.
local turret = workspace.Turret1 --the local turret in workspace local range = math.huge
local function GetClosestHumanoid() local closestHumanoid; local closestDistance = 50 -- Distance between rocket and Player
local players = game.Players:GetPlayers() --If player is detected local minions = workspace.RedMinion for i = 1, #players do local character = players[i].Character if character then local torso = character:FindFirstChild("Torso") if torso then local distance = (turret.Position-torso.Position).magnitude if distance < closestDistance then local humanoid = character:FindFirstChild("Humanoid") if humanoid and humanoid.Health > 0 then closestHumanoid = humanoid closestDistance = distance end end end end end return closestHumanoid end
--END OF SCRIPT.
Note: I left out the "Missile Propulsion" portion of the script since it's not the main focus of my problem.
Additionally, my NPCs are regular humanoids located in the Workspace; where their torso can be identified using workspace.NPC:FindFirstChild("Torso") or workspace.NPC.Torso.
|
|
|
| Report Abuse |
|
|
| |
|
|
| 17 Dec 2016 04:18 PM |
Maybe not
Group the NPCs into a model and then loop through that model |
|
|
| Report Abuse |
|
|
Nicosarea
|
  |
| Joined: 07 Dec 2007 |
| Total Posts: 4424 |
|
|
| 17 Dec 2016 05:07 PM |
Thanks for the reply! Would I change my local players to "Workspace.NPCs" instead of "game.Players:GetPlayers()"? Sorry if I sound inexperienced.
Also, what does pged mean? |
|
|
| Report Abuse |
|
|
Richast
|
  |
| Joined: 26 Nov 2011 |
| Total Posts: 192 |
|
|
| 17 Dec 2016 05:10 PM |
| Yeah, sortof. Change the line "local players = game.Players:GetPlayers()" to something like "local NPCs = workspace.NPCs" (or whatever your model is called with the NPCs in it). You don't have to change the name of the variable, but if you do be sure to change it throughout the rest of the script. |
|
|
| Report Abuse |
|
|
Nicosarea
|
  |
| Joined: 07 Dec 2007 |
| Total Posts: 4424 |
|
|
| 17 Dec 2016 05:12 PM |
| Thanks for the fast reply, Richast! Truly appreciate the help. Will test out later and record my results :) |
|
|
| Report Abuse |
|
|
Richast
|
  |
| Joined: 26 Nov 2011 |
| Total Posts: 192 |
|
|
| 17 Dec 2016 05:12 PM |
| Oh, whoops. Change it to "local NPCs = workspace.NPCs:GetChildren()" |
|
|
| Report Abuse |
|
|
Nicosarea
|
  |
| Joined: 07 Dec 2007 |
| Total Posts: 4424 |
|
|
| 17 Dec 2016 05:27 PM |
Just got the results! I received an error where the output says "Character is not a valid member of Part", on line "local character = minions[i].Character".
I believe the Character class only applies to players. Should I just remove this line? |
|
|
| Report Abuse |
|
|
Richast
|
  |
| Joined: 26 Nov 2011 |
| Total Posts: 192 |
|
|
| 17 Dec 2016 05:29 PM |
Oh, yes. Sorry I missed that.
Get rid of local character = players[i].Character if character then
and delete an "end" at the end of that function. |
|
|
| Report Abuse |
|
|
Richast
|
  |
| Joined: 26 Nov 2011 |
| Total Posts: 192 |
|
|
| 17 Dec 2016 05:30 PM |
| And the end of the loop rather, don't get rid of the end at the end of the function. Sorry I can't see the code while I'm writing these. |
|
|
| Report Abuse |
|
|
Nicosarea
|
  |
| Joined: 07 Dec 2007 |
| Total Posts: 4424 |
|
|
| 17 Dec 2016 05:33 PM |
Cool! Thanks for the help. I'll make a handful of modifications to this script to allow it to detect NPCs in the workspace.
Will post back results and paste the modified script if it's running as intended. |
|
|
| Report Abuse |
|
|
|
| 17 Dec 2016 05:34 PM |
Change it to local character = npcs[i] |
|
|
| Report Abuse |
|
|