generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: Modify This Script to Detect NPCs instead of Players?

Previous Thread :: Next Thread 
Nicosarea is not online. 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
10MilesWide is not online. 10MilesWide
Joined: 06 Aug 2014
Total Posts: 2220
17 Dec 2016 04:12 PM
pged
Report Abuse
10MilesWide is not online. 10MilesWide
Joined: 06 Aug 2014
Total Posts: 2220
17 Dec 2016 04:18 PM
Maybe not

Group the NPCs into a model and then loop through that model
Report Abuse
Nicosarea is not online. 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 is not online. 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 is not online. 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 is not online. 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 is not online. 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 is not online. 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 is not online. 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 is not online. 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
10MilesWide is not online. 10MilesWide
Joined: 06 Aug 2014
Total Posts: 2220
17 Dec 2016 05:34 PM
Change it to
local character = npcs[i]
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image