Attempt to perform arithmetic (sub) on Instance and Vector3

Hello devs!

From my other post called “Creating a Director AI, Possible?” I got a solution but i got a error once i spawned zombies, And it wasn’t straight connecting with the zombies.

The lines was this:

local lineOfSightRaycastParams = RaycastParams.new()

function hasLineOfSight(pointA, pointB)
	local result = game.Workspace:Raycast(pointA, pointB - pointA, lineOfSightRaycastParams)
	return result == nil
end
function isSpawnPointInSightOfAnyPlayer(spawnPoint)
	for _, player in pairs(game.Players:GetPlayers()) do
		-- Dead or otherwise unspawned players have no character
		-- TODO: Handle these cases so 
		local head = player.Character and player.Character:FindFirstChild("Head")
		if not head then continue end

		if hasLineOfSight(head.Position, spawnPoint) then
			return true
		end
	end

	return false
end
while wait(0.05) do
	for i,v in pairs(game.Workspace.SpawnPoints:GetChildren()) do
		if isSpawnPointInSightOfAnyPlayer(v) == false then
			InfectedModule.SpawnZombie(v)
		end
	end
end

Is there any way to fix this?

2 Likes

What are pointA and pointB in this case?

The spawn point you are using in this line:

Is a part and not a position, so you may want to change it to this:

InfectedModule.SpawnZombie(v.Position)

That’s the infected module, not the director himself

It has nothing todo with the error i’m having, Although the module does this line here: NewCommon.HumanoidRootPart.CFrame = spawnpoint.CFrame

hasLineOfSight(head.Position, spawnPoint)

head.Position - pointA
spawnPoint - pointB

Did you mean to put spawnPoint.Position?

image
When i do that, Zombies starts infinitely spawning even when i look at it

Give me a second. (yup still doesnt work, i changed the cooldown less)

No debounce/cooldown system present?

I’ve changed the while wait to while wait(2)

Makes it slower but… no. None

Does it still throw the error?

No, But when i look at it, It still spawns zombies.

while wait() do
	for i,v in pairs(game.Workspace.SpawnPoints:GetChildren()) do
		if isSpawnPointInSightOfAnyPlayer(v) == false then
			InfectedModule.SpawnZombie(v)
		end
		wait(1)
	end
end

Add the wait here.

You forgot the isSpawnPointInSightOfAnyPlayer(v.Position) but it’s ok, i added it back.

Confusing, i believe the issue is that it is meant to spawn zombies whenever he looks away from the spawn?

Yeah, I just copied it from your original code to show where the wait() should go.

Right, but if it’s spawning too many then he should add a cooldown.

1 Like

Yes. It actually means that.

It is like L4D2’s director ai but only spawns zombies

You can also use .CFrame instead of .Position by the way.

I believe there was a different way for checking the line of site for an npc/player, where you use Unit on a vector3 value and also use :Dot(). I haven’t looked into it, but i had seen it in a tutorial by a few youtubers. Might that help?

CFrame is not working which that means it only working with position.

It’s a raycast, What you expect?