I have code here for when the player clicks start. The problem is I haven't tested it and I don't know if it will work. I would like for someone to look and see if I made any mistakes. I would also like to know what the code should be for line 34. Thx in advance!

btw: The two places are from the same game but different places.

LocalScript in menu GUI. Look in the Start function.

local uis = game:GetService('UserInputService')
local tps = game:GetService('TeleportService')
local label = script.Parent.StartLabel
local MS = game:GetService('MessagingService')
local debounce = false
local topic = 'FindAvailableServer'

local placeId = 0

if uis.GamepadEnabled then
	label.Text = 'Press R2/B2 to play'
elseif uis.KeyboardEnabled then
	label.Text = 'Press Enter/Return to play'
elseif uis.TouchEnabled then
	label.Text = 'Tap to play'
end

local function Start()
	debounce = true
	
	script.Parent.StartLabel.Text = 'Searching for Server...'
	
	MS:PublishAsync(topic, 'Player-'..game.Players.LocalPlayer.UserId)
	
	local GameFound
	local Connection = MS:SubscribeAsync('Player-'..game.Players.LocalPlayer.UserId, function(message)
		GameFound = message
	end)
	Connection:Disconnect()
	
	if not GameFound then
		script.Parent.StartLabel.Text = 'Unable to locate a server... Creating one'
		--Create a new server and teleport the player to that server
		script.Parent.StartLabel.Text = 'Server Created! Teleporting...'
	else
		script.Parent.StartLabel.Text = 'Server Located! Teleporting...'
		tps:TeleportToPlaceInstance(placeId, GameFound, game.Players.LocalPlayer)
	end
	
	debounce = false
end

uis.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.ButtonR2 or input.UserInputType == Enum.UserInputType.Touch or input.KeyCode == Enum.KeyCode.Return then
		if not debounce then
			Start()
		end
	end
end)

This is a Script in another place in the ServerScriptService

local MS = game:GetService('MessagingService')
local topic = 'FindAvailableServer'

while true do
	wait()
	local Connection = MS:SubscribeAsync(topic, function(message)
		if workspace.GameInProgress.Value then
			MS:PublishAsync(message, nil)
		else
			MS:PublishAsync(message, game.JobId)
		end 
	end)
	Connection:Disconnect()
end

2 comments

0

Because you're disconnecting the event listener immediately, the event listener will never actually listen for anything... I think you must've used the loop to compensate for that, but it just doesn't work that way.

0

OK, I didn't know how to use MessagingService, and testing it is a hassle, so thx for correcting where I went wrong