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 » Scripting Helpers
Home Search
 

Re: How can I edit this script so it follows Players only?

Previous Thread :: Next Thread 
RoScripts is not online. RoScripts
Joined: 27 Oct 2012
Total Posts: 1824
29 Oct 2012 08:46 PM
It currently follows Players and other Zombies too. I've tried editing it, but I can't seem to make it work. Can anyone please help? And yes I took this script from free models.

local larm = script.Parent:FindFirstChild("Left Arm")
local rarm = script.Parent:FindFirstChild("Right Arm")

function findNearestTorso(pos)
local list = game.Workspace:children()
local torso = nil
local dist = 1000
local temp = nil
local human = nil
local temp2 = nil
for x = 1, #list do
temp2 = list[x]
if (temp2.className == "Model") and (temp2 ~= script.Parent.Parent) then
temp = temp2:findFirstChild("Torso")
human = temp2:findFirstChild("Humanoid")
if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
if (temp.Position - pos).magnitude < dist then
torso = temp
dist = (temp.Position - pos).magnitude
end
end
end
end
return torso
end

while true do
wait(0.1)
local target = findNearestTorso(script.Parent.Parent.Torso.Position)
if target ~= nil then
script.Parent.Parent.Humanoid:MoveTo(target.Position, target)
end
end
Report Abuse
gamer961 is not online. gamer961
Joined: 20 Jul 2011
Total Posts: 800
29 Oct 2012 08:49 PM
Rename Zombie Torsos maybe?
Report Abuse
RoScripts is not online. RoScripts
Joined: 27 Oct 2012
Total Posts: 1824
29 Oct 2012 08:52 PM
@Gamer

I tried that. Complete mess. Arms kept falling off, scripts wouldn't work anymore, etc.

I don't want to go back to that again.
Report Abuse
1pie23 is not online. 1pie23
Joined: 11 Jul 2010
Total Posts: 1865
29 Oct 2012 08:54 PM
local larm = script.Parent:FindFirstChild("Left Arm")
local rarm = script.Parent:FindFirstChild("Right Arm")

function findNearestTorso(pos)
local list = game.Workspace:children()
local torso = nil
local dist = 1000
local temp = nil
local human = nil
local temp2 = nil
for x = 1, #list do
temp2 = list[x]
if (temp2.className == "Model") and (temp2 ~= script.Parent.Parent) then
temp = temp2:findFirstChild("Torso")
human = temp2:findFirstChild("Humanoid")
if (temp ~= nil) and (human ~= nil) and (human.Health > 0) and (game.Players:findFirstChild(human.Parent.Name) then
if (temp.Position - pos).magnitude < dist then
torso = temp
dist = (temp.Position - pos).magnitude
end
end
end
end
return torso
end

while true do
wait(0.1)
local target = findNearestTorso(script.Parent.Parent.Torso.Position)
if target ~= nil then
script.Parent.Parent.Humanoid:MoveTo(target.Position, target)
end
end
Report Abuse
RoScripts is not online. RoScripts
Joined: 27 Oct 2012
Total Posts: 1824
29 Oct 2012 09:24 PM
That didn't work. :/
Report Abuse
RoScripts is not online. RoScripts
Joined: 27 Oct 2012
Total Posts: 1824
29 Oct 2012 09:42 PM
Bump.
Report Abuse
fireblade2 is not online. fireblade2
Joined: 18 Nov 2007
Total Posts: 4666
29 Oct 2012 09:44 PM
local larm = script.Parent:FindFirstChild("Left Arm")
local rarm = script.Parent:FindFirstChild("Right Arm")

function findNearestTorso(pos)
local list = game.Workspace:children()
local torso = nil
local dist = 1000
local temp = nil
local human = nil
local temp2 = nil
for x = 1, #list do
temp2 = list[x]
if (temp2.className == "Model") and (temp2 ~= script.Parent.Parent) then
if game:findFirstChild("Players") then
if game:findFirstChild("Players"):findFirstChild(temp2.Name) then
if game:findFirstChild("Players"):findFirstChild(temp2.Name).className == "Player" then
temp = temp2:findFirstChild("Torso")
human = temp2:findFirstChild("Humanoid")
if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
if (temp.Position - pos).magnitude < dist then
torso = temp
dist = (temp.Position - pos).magnitude
end
end
end
end
end
end
end
return torso
end

while true do
wait(0.1)
local target = findNearestTorso(script.Parent.Parent.Torso.Position)
if target ~= nil then
script.Parent.Parent.Humanoid:MoveTo(target.Position, target)
end
end
Report Abuse
BJCarpenter is not online. BJCarpenter
Joined: 04 Nov 2008
Total Posts: 4416
29 Oct 2012 10:02 PM
In case those don't work. This is how I've been doing it (but I like the one that checks if TempTarget is Player-Class)

local AI = script.Parent
local AIName = AI.Name
.
.
.
if (temp2.className == "Model") and (temp2 ~= script.Parent.Parent) and Temp2.Name -= AIName then

Report Abuse
RoScripts is not online. RoScripts
Joined: 27 Oct 2012
Total Posts: 1824
29 Oct 2012 10:14 PM
@FireBlade

I honestly can't thank you enough! I love you! (Literally, lol)

Would you like a copy of the project I'm doing?

@BJ

Thank you for the help, Fire already solved it.
Report Abuse
fireblade2 is not online. fireblade2
Joined: 18 Nov 2007
Total Posts: 4666
29 Oct 2012 10:17 PM
xD No, I just added a few lines of code (your thanks is enough of a payment). Have fun with your game.

Just a fair warning:

You may want to be careful what you name your AIs because things could get messy if you name one of them a player's username (which is one flaw in the solution I showed you).
Report Abuse
SCARFACIAL is not online. SCARFACIAL
Joined: 28 Jan 2010
Total Posts: 7970
29 Oct 2012 10:24 PM
I see you already have a working solution, but I'd also like to show you how I would do this:

do
   local Players = Game:GetService("Players")
   function GetClosestPlayer()
      local Distance, Player = math.huge
      for i,v in pairs(Players:GetPlayers()) do
         local DistanceFromZombie = v:DistanceFromCharacter( script.Parent:GetModelCFrame().p )
         if v.Character and DistanceFromZombie < Distance then
            Distance = DistanceFromZombie
            Player = v
         end
      end
      return Player
   end
end

This would only have to iterate through the Players rather than every object in the game, thus being more efficient. My version is also shorter and easier on the eyes. ;P
Report Abuse
RoScripts is not online. RoScripts
Joined: 27 Oct 2012
Total Posts: 1824
29 Oct 2012 10:34 PM
@Fire

Thanks! Are you sure you don't want a copy?

@Scar

I'll just take a sample of this script just in case I need it. And thanks!
Report Abuse
fireblade2 is not online. fireblade2
Joined: 18 Nov 2007
Total Posts: 4666
29 Oct 2012 10:36 PM
No, thanks. Like I said before, just have fun with it.
Report Abuse
RoScripts is not online. RoScripts
Joined: 27 Oct 2012
Total Posts: 1824
29 Oct 2012 10:45 PM
Well thank you very much. If you need me (Or any favors) hit me up on my main. I'll message you my main. I don't like people knowing it.
Report Abuse
BJCarpenter is not online. BJCarpenter
Joined: 04 Nov 2008
Total Posts: 4416
29 Oct 2012 11:07 PM
Thanks Scar,

I always wondered why that FindNearest, check EVERYTHING.

I assume that this is tested, in a working Model. I'll replace the FindNeaest in my Speed AI. See if I get any bug reports.

Thanks,
Brain
Report Abuse
BJCarpenter is not online. BJCarpenter
Joined: 04 Nov 2008
Total Posts: 4416
29 Oct 2012 11:20 PM
ScarFacial,

Why don't u check if Player is alive, and if he has a Torso or not?

Is that not nessasary?
Brian

Report Abuse
fatcarlos8 is not online. fatcarlos8
Joined: 25 Dec 2010
Total Posts: 563
30 Oct 2012 05:19 AM
...Dont have a clue...
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • 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