Getting status variable into a ui

Hi,

I’m making a game and I’m having problems trying to figure out how to put the status in a textlabel from a server sided module. What is the most efficient way to get the status into a ui?

-- handles the game server side.
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Assets = ReplicatedStorage.Assets
local Packages = ReplicatedStorage.Packages
local Source = ReplicatedStorage.Source

local BridgeNet2 = require(Source.Dependencies.BridgeNet2)
local Settings = require(Source.Dependencies.GameSettings)
local Knit = require(Packages.Knit)

local GameService = Knit.CreateService{
	Name = "GameService",
	Client = {},
}

local StatusBridge = BridgeNet2.ReferenceBridge("StatusBridge")

local Status = "2+ needed to start. press play!"
local Began = false
	
local function gamePresync() -- starts up the game
	-- count amount of people in dead (dead = players playing.)
	local Break = false
	
	Settings.Dead.PlayerAdded:Connect(function(player: Player) 
		local playerCount = #Settings.Dead:GetPlayers()
		
		if playerCount >= Settings.MinimumPlayers then
			Break = false
			-- 1-3 = 1 seekers, 4-6 = 2 seekers, 7-9 = 3 seekers.
			local seekerCount = math.ceil(playerCount / 3)
			-- start intermission
			for seconds = Settings.Intermission, 0, -1 do
				Status = "starting in: "..seconds.." seconds."
				
				if Break then
					Status = "2+ needed to start. press play!"
					break
				end
				
				task.wait(1)
			end
			
		end
	end)
	
	Settings.Dead.PlayerRemoved:Connect(function(player: Player) 
		local playerCount = #Settings.Dead:GetPlayers()
		
		if playerCount < Settings.MinimumPlayers and not Began then
			Break = true
		end
	end)
end

gamePresync()

return GameService

You could have a remote event that the server invokes whenever the status changes(when you set the status), and in the handler you can then change the textlabel, or you could have a remote function periodically (or when required) request the server for the current status, but that would not be efficient. I’d go with the former.

The “status” approach typically involves a StringValue, which is placed in ReplicatedStorage so both the server and client may access it. Your script will write the status to the StringValue, which will naturally replicate to the client. The client can detect changes in its “Value” property with the instance’s “Changed” event. The function connected to this event is provided with the new state of the property. Build a named function that takes a current status string and writes it to your TextLabel. Manually invoke it with the StringValue’s current value to initialize the TextLabel, then connect the same function to the aforementioned event for continued updates:

local function updateStatus(status: string)
    -- ...
end
updateStatus(Status.Value)

Status.Changed:Connect(updateStatus)
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.