NPC's deleting themselves for no reason at all

I have created an NPC system using context steering, it works great. Though for some strange bizarre reason, the NPC’s keep deleting themselves. This only happens to NPC’s which contain the same script, though, there is no where in the script which is deleting the NPC’s. No where.

https://gyazo.com/9fa397ce8ea4a666824fb7bf8d535ed7

I have no idea why they are doing this, I have scoured my game for the last hours, searching for any line of code which could delete the NPC’s. But to no avail. I wondered if it had to do with script performance maybe? Though the scripts are performing well, less than 0.3% each. Has anyone else suffered this issue?

Mind sharing your script for this? (NPC system). Also, this is just a guess because I haven’t seen your script yet but I believe it could be to do with the same scripts overriding each other and therefore causing bugs like that.

1 Like

Hmm could you provide some more information, where are the npcs stored at in workspace? is it possible the parent of the npcs is getting deleted? It seems like the npcs get deleted at the same time, which is a sign that it could be the scenario. Is the humanoidRootPart anchored, and is there a chance the npcs are getting flinged for some reason?

1 Like
local NPC = script.Parent
local RootPart = NPC:FindFirstChild("HumanoidRootPart")
local Humanoid = NPC:FindFirstChild("Humanoid")
local AlignOrientation = RootPart:FindFirstChild("AlignOrientation")

RootPart:SetNetworkOwner(nil)

local Velocity = Vector3.zero
local SteeringForce = Vector3.zero

local Directions = {{1, -1}, {1, 1}, {-1, 1}, {-1, -1}}

local MaxSpeed = 16
local MaxForce = 5
local AvoidForce = 50
local ObstacleRange = 2.5

local Swinging = false
local t = tick()

local AttachmentHolder = workspace:WaitForChild("AttachmentHolder")
local Attachment = Instance.new("Attachment")
Attachment.Parent = AttachmentHolder
AlignOrientation.Attachment1 = Attachment


function GetRaycast(Origin, Direction)
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {NPC, workspace.Characters}
	
	local Raycast = workspace:Raycast(Origin, Direction, raycastParams)
	
	if Raycast then
		return Raycast
	end
	
	return false
end

function GetTargetDirection(OriginPosition, TargetPosition)
	local Direction = (TargetPosition - OriginPosition)
	
	return Direction
end

function GetTargetLook(TargetPosition, Magnitude, Offset)
	return CFrame.lookAt(RootPart.Position, TargetPosition) * CFrame.new(0, 0, (-Magnitude + Offset)).Position
end

function LimitMagnitude(Vector, Max)
	local Limit = Vector.Unit * math.min(Vector.Magnitude, Max)
	
	return Limit
end

function SeekForce(TargetVector)
	local NewVector = GetTargetDirection(RootPart.Position, TargetVector)
	NewVector = NewVector.Unit * MaxSpeed
	NewVector -= Velocity
	NewVector = LimitMagnitude(NewVector, MaxForce)
	
	return NewVector
end

function FleeForce(TargetVector)
	local NewVector = GetTargetDirection(RootPart.Position, TargetVector)
	NewVector = NewVector.Unit * MaxSpeed
	NewVector = -NewVector
	
	NewVector -= Velocity
	NewVector = LimitMagnitude(NewVector, MaxForce)

	return NewVector
end

function PursueForce(Target)
	local TargetVelocity = Target.AssemblyLinearVelocity
	TargetVelocity *= 10
	TargetVelocity += Target.Position
	
	return SeekForce(TargetVelocity)
end

function EvadeForce(Target)
	local TargetVelocity = Target.AssemblyLinearVelocity
	TargetVelocity *= 10
	TargetVelocity += Target.Position

	return FleeForce(TargetVelocity)
end

function ArriveForce(TargetVector, SlowingRadius)
	local NewVector = GetTargetDirection(RootPart.Position, TargetVector)
	local Distance = NewVector.Magnitude
	
	if(Distance > SlowingRadius) then
		NewVector = NewVector * MaxSpeed
	else
		NewVector = NewVector.Unit * (MaxSpeed * (Distance/SlowingRadius))
	end

	NewVector -= Velocity
	NewVector = LimitMagnitude(NewVector, MaxForce)

	return NewVector
end

function SurroundForce()
	
end

function AvoidObstacleForce()
	for i,Direction in pairs(Directions) do
		local Direction = ((RootPart.Position + Vector3.new(Direction[1], 0, Direction[2])) - RootPart.Position).Unit * ObstacleRange
		local rayCast = GetRaycast(RootPart.Position, Direction)
		
		if rayCast then
			return (FleeForce(rayCast.Position))
		end
	end
	
	return Vector3.new(0,0,0)
end

This is the code in each NPC

The NPC’s are stored in workspace, the HumanoidRootPart is unanchored, and all items worn by the NPC’s are set to CanCollide off.

(I have just checked, every part inside the NPC is being destroyed, except non-physical instances)

Hm, I think the issue is clearly something to do with your character spazzing out and falling out of the map. I think its either to do with the velocity/force, or more likely to do with you forcing the NPC’s direction in which they’re looking at causing them to fling. On second thought, it’s likely to be with the NPC direction (orientation you know what i mean).

3 Likes

Hmm, could you maybe try to track where their positions are until they delete themselfs? I have a decent feeling they are somehow getting deleted by their position dropping below fallenpartsdestroyheight

You are correct. I’ve ran through the code and have found Limit Magnitude is returning Vector3.new(nan, nan, nan), which is most definitely sending them into the void.

1 Like

Yeah that was it lmao, thanks.

Oh, i went to sleep lol. No problem.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.