Creating the PlayerClient Script

A client script will update the UI based on the progress of the nav mesh. When the nav mesh has been generated, the client script will handle hiding the UI for the player so they can then play the game.

Create PlayerClient Script

Create a new script called PlayerClient and place it in the Client Scripts folder in the Hierarchy.

Add Custom Properties

The client script will need custom properties so it can read the dynamic properties, update the progress text, and hide the container.

  1. Add the UI Container as a custom property, and name it UIContainer.
  2. Add the Generated Map folder as a custom property, and name it GeneratedMap.
  3. Add the Progress text component as a custom property, and name it Progress.

Edit PlayerClient Script

Open up the PlayerClient script so you can edit it and create the code.

Add Variables

Add the following variables to the script so you have references to the custom properties you added.

local UI_CONTAINER = script:GetCustomProperty("UIContainer"):WaitForObject()
local GENERATED_MAP = script:GetCustomProperty("GeneratedMap"):WaitForObject()
local PROGRESS = script:GetCustomProperty("Progress"):WaitForObject()Code language: Lua (lua)

Create HideUI Function

Create a function called HideUI that will turn the visibility of the UI Container off.

local function HideUI()
	UI_CONTAINER.visibility = Visibility.FORCE_OFF
endCode language: Lua (lua)

Create UpdateProgress Function

Create a function called UpdateProgress. This function will be responsible for updating the progress text. It will get called when the dynamic custom properties change.

Because there are 2 properties, the first one for the grid needs to be divided by 2 so it contributes 50% to the progress. When the mesh is in progress, you can divide the val by 2 and add 50 to give you the total progress between 0 and 100%.

local function UpdateProgress(obj, prop)
	if prop == "grid" then
		local val = GENERATED_MAP:GetCustomProperty("grid")

		if val > 0 then
			PROGRESS.text = string.format("%.2f%%", (GENERATED_MAP:GetCustomProperty("grid") / 2) * 100)
		end
	elseif prop == "mesh" then
		local val = GENERATED_MAP:GetCustomProperty("mesh")

		if val > 0 then
			PROGRESS.text = string.format("%.2f%%", 50 + (val / 2) * 100)
		end
	end
endCode language: Lua (lua)

Connect Property Changed Event

When the dynamic custom property value changes, it will fire the customPropertyChangedEvent. By connecting a function to this event, you can listen to those changes to update the progress text.

GENERATED_MAP.customPropertyChangedEvent:Connect(UpdateProgress)Code language: Lua (lua)

Call UpdateProgress Function

2 calls to the UpdateProgress function for the grid and mesh are recommended to do in case the changed event has not connected. This could be because the player joined later and the nav mesh has been generated.

UpdateProgress(GENERATED_MAP, "grid")
UpdateProgress(GENERATED_MAP, "mesh")Code language: Lua (lua)

Connect Broadcast Event

The server will broadcast to the player to hide the UI, so you will need to connect that here.

Events.Connect("HideUI", HideUI)Code language: Lua (lua)

The PlayerClient Script

PlayerClient

local UI_CONTAINER = script:GetCustomProperty("UIContainer"):WaitForObject()
local GENERATED_MAP = script:GetCustomProperty("GeneratedMap"):WaitForObject()
local PROGRESS = script:GetCustomProperty("Progress"):WaitForObject()

local function HideUI()
	UI_CONTAINER.visibility = Visibility.FORCE_OFF
end

local function UpdateProgress(obj, prop)
	if prop == "grid" then
		local val = GENERATED_MAP:GetCustomProperty("grid")

		if val > 0 then
			PROGRESS.text = string.format("%.2f%%", (GENERATED_MAP:GetCustomProperty("grid") / 2) * 100)
		end
	elseif prop == "mesh" then
		local val = GENERATED_MAP:GetCustomProperty("mesh")

		if val > 0 then
			PROGRESS.text = string.format("%.2f%%", 50 + (val / 2) * 100)
		end
	end
end

GENERATED_MAP.customPropertyChangedEvent:Connect(UpdateProgress)

UpdateProgress(GENERATED_MAP, "grid")
UpdateProgress(GENERATED_MAP, "mesh")

Events.Connect("HideUI", HideUI)Code language: Lua (lua)
Scroll to Top