Help with density spawning

Hello!
While I am working on an open-world survival, I find myself aware of something that I don’t like about open-world survivals. The issue which I am talking about is spawning far away from a friend whenever I die in an open world survival, which after too many times will cause me to get mad and decide to quit the game. So, my solution is as follows:

  • The game will have 4 factions, and you’ll be able to pick the faction you’d like to associate with
  • As there’s no official spawn for a faction since it’s open world survival, the spawn coordinates will be based around the density of a faction within the map.

For instance, if the majority of faction is in a city in the open-world map, then I will spawn around the city. I have the spawn button as a function that activates a function that determines the appropriate location to spawn. All I need is coordinates of just one person that is part of the bulk of a faction, and it’ll be put into the function I’ve built to determine a good location to spawn from.

To simplify:
You click the spawn button → It runs the function to detemine the most densely packed area of your faction → My algorithm I built to figure out the right area to spawn around the coordinates

I need step 2, but I’m too braindead to figure it out. To detemine a person as being in the same faction, use the function isInFaction(yourPlayer, otherPlayer) and it will return whether or not a player is in your faction as true or false. Anything is helpful. You don’t need to paste code, I just need at least some sort of pseudocode to help me.

Thank you so much for your time :slight_smile:

2 Likes

You could use Region3 to see if there are any HumanoidRootParts in the area.

1 Like

Although region3 would be very nice, the issue is that tha map is extremely massive (12k x 12k studs), so it is not optimal to have a lot of regions to figure out which one has the most density :frowning:

1 Like

Perhaps keep the spawns in a table and loop through the table to see which value is highest. For example;

local areas = {
    City = 10,
    Desert = 2,
    Country = 5
}

local nextSpawn, highestDensity = "", 0
for area, density in next, areas do
    if density > highestDensity then
        nextSpawn = area
        highestDensity = density
    end
end

There are plenty of ways to do this.

1 Like

You could record players position like every 30s if you have like 100 spawning squares, just see if a square if occupied or not.

Thats perfect! Thank you very much!