Update Player Spawning Logic

Now that you have nav mesh generation working, the logic for spawning the players in the PlayerServer script needs to be updated.

Create Variables

In the PlayerServer script, add 2 new variables to the top of the script that will hold the spawn point position and rotation when they have been received. This will then be used for players who join late.

local spawnPointPosition = nil
local spawnPointRotation = nil
Code language: Lua (lua)

Create OnPlayerJoined Function

Create a new function that will be called when the player joins the game. This function will look and see if the spawnPointPostion is nil. If it is not nil then the player will be spawned at the position set in the spawnPointPosition variable. If the variable is nil, then the player will be despawned, meaning that the map and nav mesh generation is still in progress.

local function OnPlayerJoined(player)
	if spawnPointPosition ~= nil then
		player:SetWorldPosition(spawnPointPosition)
		player:SetWorldRotation(spawnPointRotation)
	else
		player:Despawn()
	end
end
Code language: Lua (lua)

Update SpawnPlayers function

The SpawnPlayers function will need to be updated so that it sets the spawnPointPosition and spawnPointRotation variables. This will be called when the nav mesh generation has been completed.

local function SpawnPlayers(position, rotation)
	spawnPointPosition = position + (Vector3.UP * 150)
	spawnPointRotation = rotation

	for _, player in ipairs(Game.GetPlayers()) do
		player:Spawn({
	
			position = spawnPointPosition,
			rotation = rotation

		})
	end
end
Code language: Lua (lua)

Connect Player Joined Event

Connect the OnPlayerJoined function to the playerJoinedEvent so it checks each player that joins the game if they can be spawned in or not.

Game.playerJoinedEvent:Connect(OnPlayerJoined)
Code language: Lua (lua)

The PlayerServer Script

PlayerServer

local spawnPointPosition = nil
local spawnPointRotation = nil

local function ActionPressed(player, action)
	if(action == "Toggle Fly Mode") then
		if(player.isFlying) then
			player:ActivateWalking()
		else
			player:ActivateFlying()
		end
	end
end

local function OnPlayerJoined(player)
	if spawnPointPosition ~= nil then
		player:SetWorldPosition(spawnPointPosition)
		player:SetWorldRotation(spawnPointRotation)
	else
		player:Despawn()
	end
end

local function SpawnPlayers(position, rotation)
	spawnPointPosition = position + (Vector3.UP * 150)
	spawnPointRotation = rotation

	for _, player in ipairs(Game.GetPlayers()) do
		player:Spawn({
	
			position = spawnPointPosition,
			rotation = rotation

		})
	end
end

Input.actionPressedEvent:Connect(ActionPressed)
Events.Connect("SpawnPlayers", SpawnPlayers)
Game.playerJoinedEvent:Connect(OnPlayerJoined)
Code language: Lua (lua)

Test the Game

Test the game and make sure the following work:

  1. The player is not spawned while the map and nav mesh is being generated.
  2. The Player is spawned after the nav mesh has been generated. You can look at the Event Log for the progress.
  3. The enemies no longer walk through walls.

Nav Mesh Settings

The nav mesh generator and manager has settings you can change if you want to lower the size of the tile to get a tighter nav mesh, and also to see the grid at runtime for debugging. Change the custom property TileSize on the DDNavMeshGenerator group in the Hierarchy for smaller tiles, and enable AutoStartNavMeshVisualization to see the grid at runtime.

Scroll to Top