Finding the closest part to a player?

Hey y’all. I’ve been working on this zombies shooter game where the zombies spawn in waves. It’s been pretty good in terms of development so far up until this point. You see, my zombies don’t have smart pathfinding, so any wall that’s between them and the player is game over. My quick solution was to simply create as many spawners as I can, so no matter where the player is, there is always a zombie spawner within direct sight. One issue though is my current function has the script randomly pick one of these zombie spawners and spawn the zombie there. Obviously that won’t work considering the zombies can spawn in random parts across the map. Also there are doors you have to unlock with money, so if the plaeyr has no money and the zombie spawns on the other side, the game can’t continue.

So is there a way to script it so the game finds the closest spawner to the player out of all other spawners, and has the zombie spawn there?

2 Likes

The method you’re mentioning is incredibly inefficient, unpredictable and will probably take a lot of resources. There is a wiki page on pathfinding that’s really informative. I suggest you use that instead as it will produce better results.

2 Likes

Hey there,

By reading your text I saw two issues that you were facing:

  1. Making sure that only spawners in regions that are unlocked will be used,
  2. Finding the closest spawn to the player.

For both problems, there are multiple ways on how to solve it.
For the first issue you could, for instance, make a model with all spawners that are unlocked, once a new region is unlocked new spawns will be inserted into that model or there could be a simple BoolValue inside of the spawn that says whether or not this spawn is active.

For the second problem, there are also multiple ways to achieve this.
I would personally use magnitude.
This will probably not be the most efficient way.

local Spawns = workspace.Spawns -- A model in workspace with all spawns.
local Closest -- This will be set as the closest spawn to the player
local PlayerPosition = Character.PrimaryPart.Position

for i,v in pairs(Spawns:GetChildren()) do
	if Closest == nil then
		Closest = v
	else
		if (PlayerPosition - v.Position).magnitude < (Closest.Position - PlayerPosition).magnitude then
			Closest = v
		end
	end
end


ClosestSpawn.rbxl (19.3 KB)


Let me know if this helps you :slight_smile:

7 Likes

Ah I see! I think a mix of the 2 would work. I could use the BoolValue to detect if it is active, and when it changes to true, a script within the spawner could move the spawner to a folder in Workspace called CurrentSpawns, and then from there detect the playerPosition, read all children of CurrentSpawns, and from there carry out the functions. I’m pretty faithful in that so I’ll just give you a Solution check.
Thanks dude! I never knew about magnitude.

3 Likes

I really gave pathFinding as many chances as I could, but unless I want to take the time to learn how to make my own pathfinding system, a regular MoveTo() will have to do. Pathfinding is consistently behind and even with collectionService super buggy. The zombie never catches up to the player.

1 Like