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: NPC controller

Previous Thread :: Next Thread 
RecurringNightmare is not online. RecurringNightmare
Joined: 05 Jul 2012
Total Posts: 15336
27 Jan 2016 03:04 PM
So I have this script in ServerScriptService. It's to control detection of players for NPCs to follow when they get in range. It works well except for that after a while the script stops working completely. I don't think this is due to other scripts in my game but...if anyone has any ideas on how to optimize this script to make it work better, please post here.

-- Note: "NPC" is a model in workspace that nests all the NPC models

local bandits = game.Workspace.NPC;
while wait(.1) do
for _, bandit in pairs (bandits:GetChildren()) do
for _, player in pairs (game.Players:GetPlayers()) do
if bandit:FindFirstChild("Torso") and (bandit.Torso.Position - player.Character.Torso.Position).magnitude < 20 and player.Character.Humanoid.Health > 0 then
if bandit:WaitForChild("Zombie").Health >= 0 then
bandit.Zombie:MoveTo(player.Character.Torso.Position);
for _, work in pairs (game.Workspace:GetChildren()) do
if work:IsA("Part") and (work.Position - bandit.Torso.Position).magnitude <= 5 then
if work.Position.Y >= bandit.Torso.Position.Y then
bandit.Zombie.Jump = true;
end
if work:IsA("Model") then
for _, mode in pairs (work:GetChildren()) do
if work.Position.Y >= bandit.Torso.Position.Y then
bandit.Zombie.Jump = true;
end
end
end
end
end
end
end
end
end
end
Report Abuse
T20a1n14n14e5r18 is not online. T20a1n14n14e5r18
Joined: 21 Jan 2016
Total Posts: 225
27 Jan 2016 03:05 PM
You may have mistype _G as _.
Report Abuse
WolfgangVonPrinz is not online. WolfgangVonPrinz
Joined: 24 Oct 2013
Total Posts: 4656
27 Jan 2016 03:06 PM
Not sure if @Above is trolling ior...

The mountain chicken, my favourite frog.


Report Abuse
T20a1n14n14e5r18 is not online. T20a1n14n14e5r18
Joined: 21 Jan 2016
Total Posts: 225
27 Jan 2016 03:08 PM
Sort of both, I only know a little Lua, I know other languages besides Lua.
Report Abuse
RecurringNightmare is not online. RecurringNightmare
Joined: 05 Jul 2012
Total Posts: 15336
27 Jan 2016 03:09 PM
It's an in pairs loop. Instead of using an i in the general 'i,v' iterators I used an underscore
Report Abuse
KapKing47 is not online. KapKing47
Joined: 09 Sep 2012
Total Posts: 5522
27 Jan 2016 03:14 PM
For efficiency u'd need to change from pairs to numeric loop

local b = bandits:GetChildren()
for i = 1, #b do
local bandit = b[i] --just so u wont have to change anything that uses the 'bandit' variable
--then do the same for the other loops

"My Life is going Good... but..."
Report Abuse
RecurringNightmare is not online. RecurringNightmare
Joined: 05 Jul 2012
Total Posts: 15336
27 Jan 2016 03:16 PM
What makes numeric loop more efficient?
Report Abuse
KapKing47 is not online. KapKing47
Joined: 09 Sep 2012
Total Posts: 5522
27 Jan 2016 03:20 PM
Try for urself :P I remember once, I did a color test with pixels once, and used a generic loop, and then a friend looked at my code and reminded me about numeric loops, and I replaced the loop with a numeric one, and guess what... the performance % went down a lot! If I remember correctly, about twice less than with a generic loop :P

"My Life is going Good... but..."
Report Abuse
RecurringNightmare is not online. RecurringNightmare
Joined: 05 Jul 2012
Total Posts: 15336
27 Jan 2016 03:28 PM
I may try that and test o.o
Report Abuse
KapKing47 is not online. KapKing47
Joined: 09 Sep 2012
Total Posts: 5522
27 Jan 2016 03:39 PM
Do.
Also... to answer ur question on how it helps,
that's cos it's straight forward and specific, generic loops ALWAYS check if there is a next value, and they somehow get the values that are indexed with strings (never got to studying that) so ya know.

"My Life is going Good... but..."
Report Abuse
RecurringNightmare is not online. RecurringNightmare
Joined: 05 Jul 2012
Total Posts: 15336
27 Jan 2016 03:44 PM
ok
Report Abuse
RecurringNightmare is not online. RecurringNightmare
Joined: 05 Jul 2012
Total Posts: 15336
27 Jan 2016 07:42 PM
Before I used numeric iterations the script performance peaked at about 16% with 4 NPCs chasing me.

After I used numeric iterations the script performance peaked at about 4.1%-5.8% with 4 NPCs chasing me.

A lot better. Thank you very much. But still not good enough. I think it's because of the constant waiting and getplayers requesting. I changed the wait(.1) at top of the script to wait(.5) but the GetPlayers method is still inside the loop so I think that's also having an effect on the script performance..
Report Abuse
SinisterMentality is not online. SinisterMentality
Joined: 14 Jun 2015
Total Posts: 700
27 Jan 2016 07:44 PM
have u tried setting it to wumbo


~the realest hustler~
Report Abuse
RecurringNightmare is not online. RecurringNightmare
Joined: 05 Jul 2012
Total Posts: 15336
27 Jan 2016 07:45 PM
full script now:

local bandits = game.Workspace.NPC;
local b = bandits:GetChildren()

while wait(.5) do
for i = 1, #b do
local bandit = b[i]
local getplayers = game.Players:GetPlayers()
for ii = 1, #getplayers do
local player = getplayers[ii]
if bandit:FindFirstChild("Torso") and (bandit.Torso.Position - player.Character.Torso.Position).magnitude < 20 and player.Character.Humanoid.Health > 0 then
if bandit:WaitForChild("Zombie").Health >= 0 then
bandit.Zombie:MoveTo(player.Character.Torso.Position);
local workspace = game.Workspace:GetChildren()
for iii = 1, #workspace do
local work = workspace[iii]
if work:IsA("Part") and (work.Position - bandit.Torso.Position).magnitude <= 5 then
if work.Position.Y >= bandit.Torso.Position.Y then
bandit.Zombie.Jump = true;
end
if work:IsA("Model") then
local modelchildren = work:GetChildren()
for iv = 1, #modelchildren do
if work.Position.Y >= bandit.Torso.Position.Y then
bandit.Zombie.Jump = true;
end
end
end
end
end
end
end
end
end
end
Report Abuse
RecurringNightmare is not online. RecurringNightmare
Joined: 05 Jul 2012
Total Posts: 15336
27 Jan 2016 07:45 PM
ill show u wumbo
Report Abuse
RecurringNightmare is not online. RecurringNightmare
Joined: 05 Jul 2012
Total Posts: 15336
27 Jan 2016 08:20 PM
b
Report Abuse
RecurringNightmare is not online. RecurringNightmare
Joined: 05 Jul 2012
Total Posts: 15336
27 Jan 2016 09:30 PM
b1
Report Abuse
KapKing47 is not online. KapKing47
Joined: 09 Sep 2012
Total Posts: 5522
28 Jan 2016 11:03 AM
Np ;)
As for further assistance, I'm a bit tied up right now, but I'll get back to u :)

"My Life is going Good... but..."
Report Abuse
KapKing47 is not online. KapKing47
Joined: 09 Sep 2012
Total Posts: 5522
28 Jan 2016 11:10 AM
Instead of always using GetPlayers() do this

local plrs = {}

game.Players.PlayerAdded:connect(function()
plrs = game.Players:GetPlayers()
end)

game.Players.PlayerRemoving:connect(function()
plrs = game.Players:GetPlayers()
end)

and then u can just access the 'plrs' table. If u wonder why I just set GetPlayers() for 'plrs' instead of inserting/removing values from the table, that's cos I don't trust the system of using NumPlayers and stuff :/ U could try it if u want.

"My Life is going Good... but..."
Report Abuse
RecurringNightmare is not online. RecurringNightmare
Joined: 05 Jul 2012
Total Posts: 15336
28 Jan 2016 02:51 PM
This time script performance peaked at about 4.3%-4.7% with the same number (4) of NPCs chasing me. That helped a bit.
Report Abuse
bowlsroblox is not online. bowlsroblox
Joined: 03 Dec 2013
Total Posts: 133
28 Jan 2016 03:19 PM
I prefer using Pathfinding service.

Video: https://www.youtube.com/watch?v=pQ9Mcc-5UuA

That's make it more easy. Since it detects the shortest way to go to random target and avoids any obstacles.
Report Abuse
KapKing47 is not online. KapKing47
Joined: 09 Sep 2012
Total Posts: 5522
28 Jan 2016 03:51 PM
Another suggestion (thought off by reading ur code more thoroughly.
Instead of looping through all the items in Workspace and then the model in it, just raycast instead :P

"My Life is going Good... but..."
Report Abuse
RecurringNightmare is not online. RecurringNightmare
Joined: 05 Jul 2012
Total Posts: 15336
28 Jan 2016 03:54 PM
@bowls
thanks

@kap
I've heard raycasting is an expensive operation too but I can't really work with it anyway
Report Abuse
KapKing47 is not online. KapKing47
Joined: 09 Sep 2012
Total Posts: 5522
28 Jan 2016 03:56 PM
Not as expensive as looping through Workspace and then through all the models in it :P
Unless u only have like a few objects in Workspace and just a few models with not as many parts in them.

"My Life is going Good... but..."
Report Abuse
RecurringNightmare is not online. RecurringNightmare
Joined: 05 Jul 2012
Total Posts: 15336
28 Jan 2016 04:01 PM
mehh how would i use it in this code
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