Error: attempt to perform arithmetic (sub) on Vector3 and nil (Zombie Script)

I have a zombie script that was working before but seems to not work anymore due to this error.
Here is the code

function findNearestTorso(pos)
	local list = game.Workspace:GetChildren()
	local torso = nil
	local dist = 30
	local temp = nil
	local human = nil
	local temp2 = nil
	local zombie = nil
	for x = 1, #list do
		temp2 = list[x]
		if (temp2.className == "Model") and (temp2 ~= Zombie) then
			temp = temp2:findFirstChild("HumanoidRootPart") -- Checks for HumanoidRootPart 
			human = temp2:findFirstChild("Humanoid") -- Checks for Humanoid
			zombie = temp2:findFirstChild("ZombieCheck") -- Triple checks for a IntValue named "ZombieCheck"
			if (temp ~= nil) and (human ~= nil) and (human.Health > 0) and not zombie then -- If it has a HumanoidRootPart, A humanoid and doesnt have a ZombieCheck IntValue then
				if (temp.Position - pos).magnitude < dist then
					torso = temp
					dist = (temp.Position - pos).magnitude
				end
			end
		end
	end
	return torso
end
1 Like
  1. What line is the error in?
  2. How is the function being called?
1 Like

Have you defined “Zombie”? You defined “zombie” but not capitalised

Sorry, didn’t know which line the error was on

1 Like

The error is in this line

if (temp.Position - pos).magnitude < dist then

the function is being called with a remote event using this line of code

ClientEvent.OnClientEvent:Connect(findNearestTorso)

1 Like

the variable zombie is used to detect a intvalue in the Zombie or NPC
the variable Zombie is used for the main NPC or Zombie if that makes sense

1 Like

The error is in this line

if (temp.Position - pos).magnitude < dist then

the function is being called with a remote event using this line of code

ClientEvent.OnClientEvent:Connect(findNearestTorso)

I think the error is to do with the script being on the client since it works perfectly fine when used on the server

1 Like

This means that pos is nil. Can you show the script sending it to the client.

1 Like

The server script is here that clones the zombie to the server and fires the local script with the zombie movement and scripts

ClientEvent.OnServerEvent:Connect(function(player)
	print("player joined")
	ClientEvent:FireClient(player)
	if debounce == false then
		Zombie:Clone().Parent = game.Workspace.ChasingZombies
		debounce = true
	end
end)

The server script is fired from a local script

players.CharacterAdded:Connect(function(player)

ClientEvent:FireServer(player)

print("event fired")

end)
1 Like

You aren’t firing anything alongside player, so pos will be nil.
Also, why aren’t you just using CharacterAdded on the player in the server script to prevent unnecessary network traffic?

1 Like

How would I use characteradded in the serverscript to fire the event everytime a character is added.
and how would I set it out to have pos along with player

1 Like

so temp.Position is already a valid Vector3, but it seems like variable pos is nil

1 Like