Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

NPC Follow system works but after death it stops working [?]

Asked by 8 years ago
Edited 6 years ago

Goal:

My goal is to create an NPC follow system (I already have the damage system completed). What it should do it if you are within a certain range measured by magnitude you are chased by some NPC's that are within range.

Problem:

The problem is when a Player first spawns in the NPC chases the player and everything but when the player Dies by something (NPC, another player) the NPC's stop chasing the player. The code works but it stops working sometimes.

Script:

Note that this is a ServerScript in ServerScriptService

01local RunService = game:GetService("RunService")
02local Settings = require(game:GetService("ReplicatedStorage").ModuleScripts.SETTINGS)
03local Respawn_Time = Settings.BANDIT.RESPAWN_TIME
04local plrServ = game:GetService('Players')
05local NPCBackup = game:GetService('ServerStorage').NPCBackup.Bandit
06 
07local function getClosestPlayer(myPos)
08    local pos, tor = Settings.BANDIT.RANGE
09    for _, plr in pairs(plrServ:GetPlayers()) do
10        if plr.Character then
11            if (plr.Character.PrimaryPart.Position - myPos).Magnitude < pos then
12                pos = (plr.Character.PrimaryPart.Position - myPos).Magnitude
13                tor = plr.Character.PrimaryPart
14            end
15        end
View all 54 lines...

Hopefully, this problem gets solved, I appreciate any feedback that fixes this issue or improves the code. I try avoiding loops and RunService mostly because I do not know when is the correct time to use a while true do loop and I know that using a loop incorrectly can be studio crashing and game breaking. Thank you for reading.

0
Correct me if I'm wrong, but it seems like you only call the setup for each NPC once. That means when the player dies, the NPC can't find the torso and calls "return" in the else function. That would make the NPC stop moving towards the player. Creeperman1524 120 — 6y
0
I do believe that someone had said this as well on the discord Creeperman1524 120 — 6y
0
How could I fix this? BlackOrange3343 2676 — 6y
0
I would think just removing it would work. If it finds the target it will move. If it doesn't instead of never moving, it will constantly look for a new target. Creeperman1524 120 — 6y

2 answers

Log in to vote
1
Answered by 6 years ago

Although this may no be correct, here is my answer on it

01local RunService = game:GetService("RunService")
02local Settings = require(game:GetService("ReplicatedStorage").ModuleScripts.SETTINGS)
03local Respawn_Time = Settings.BANDIT.RESPAWN_TIME
04local plrServ = game:GetService('Players')
05local NPCBackup = game:GetService('ServerStorage').NPCBackup.Bandit
06 
07local function getClosestPlayer(myPos)
08    local pos, tor = Settings.BANDIT.RANGE
09    for _, plr in pairs(plrServ:GetPlayers()) do
10        if plr.Character then
11            if (plr.Character.PrimaryPart.Position - myPos).Magnitude < pos then
12                pos = (plr.Character.PrimaryPart.Position - myPos).Magnitude
13                tor = plr.Character.PrimaryPart
14            end
15    else
View all 55 lines...

You may have to return the torso's position from the getClosestPlayer function, but I don't know for sure. Hopefully this works (which it probably doesn't)! :D

0
Thank you BlackOrange3343 2676 — 6y
Ad
Log in to vote
-1
Answered by 6 years ago

Use this:

01local larm = script.Parent:FindFirstChild("Left Arm")
02local rarm = script.Parent:FindFirstChild("Right Arm")
03 
04function findNearestTorso(pos)
05    local list = game.Workspace:children()
06    local torso = nil
07    local dist = 10
08    local temp = nil
09    local human = nil
10    local temp2 = nil
11    for x = 1, #list do
12        temp2 = list[x]
13        if (temp2.className == "Model") and (temp2 ~= script.Parent) then
14            temp = temp2:findFirstChild("Torso")
15            human = temp2:findFirstChild("Humanoid")
View all 33 lines...
0
Really... You 1 took a free model, 2. Used a loop when I said I wish to prevent that, 3. Didn't even bother explaining anything (probably because you stole it) but still. Thanks for trying though I gotta give you that BlackOrange3343 2676 — 6y
0
Boy, I've been using this in RPG's for years. Zerixa_RBLX -2 — 6y
0
Lmao search up zombie in workspace and look at the script. BlackOrange3343 2676 — 6y

Answer this question