Weird Issue with My Matchmaking System (Explains the Issue in Post)

Hi! Im currently making a matchmaking system where when two players enter a zone, they get sent to a seperate game together.

Everything works perfectly fine, except for this one issue.
When one person enters the queue, every other person in the server (I think) gets added to the queue as well, when only 1 person enters it.
The goal is to to have only the players who are in the queue, to be added.

Here is my scripts:

Client-side script:

local player = script.Parent.Parent
local Zone = require(game.ReplicatedStorage:WaitForChild("Assets").Modules.Zone)
local JoinQueue = game.ReplicatedStorage:WaitForChild("Remotes").JoinQueue
local ExitQueue = game.ReplicatedStorage:WaitForChild("Remotes").ExitQueue
local Model = workspace.MatchmakingZone
local zone = Zone.new(Model)

zone.playerEntered:Connect(function()
	print(player.Name)
	JoinQueue:FireServer(player)
end)

zone.playerExited:Connect(function()
	print(player.Name)
    ExitQueue:FireServer(player)
end)

(prints are for debugging)

Server-side script:

local TeleportService = game:GetService("TeleportService")
local JoinQueue = game.ReplicatedStorage:WaitForChild("Remotes").JoinQueue
local ExitQueue = game.ReplicatedStorage:WaitForChild("Remotes").ExitQueue


local TargetPlaceID = 18124773404
local Queue = {}


JoinQueue.OnServerEvent:Connect(function(P, AddedPlayer)
	if table.find(Queue, AddedPlayer) then return end
	table.insert(Queue, AddedPlayer)
	
	print("Added "..AddedPlayer.Name.." to Queue.")
	warn(Queue)
	
	if #Queue >= 2 then
		local p1 = Queue[1]
		local p2 = Queue[2]
		
	
		
		local tpTargets = {p1, p2}
		
		local reservedServ = TeleportService:ReserveServer(TargetPlaceID)
		
		TeleportService:TeleportToPrivateServer(TargetPlaceID, reservedServ, tpTargets)
	end
end)


ExitQueue.OnServerEvent:Connect(function(P, RemovedPlayer)
	if table.find(Queue, RemovedPlayer) then
		print("Removed "..RemovedPlayer.Name.." from Queue.")
		local playerPos = table.find(Queue, RemovedPlayer)
		table.remove(Queue, playerPos)
		warn(Queue)
	end
end)

If anyone could help, that would be great!

4 Likes

ps- Im also very new to tables and such, so just keep that in mind.

2 Likes

So you have the zone playerEntered event on the client which means each client connects a handler to that event, and I assume player is a reference to the local player?

I don’t know the internals of how zone works but could it be that when a player enters the zone, the callback fires for all the clients and each client fires JoinQueue:FireServer with their own local player reference? That would explain why when 1 person enters, every other player in the server joins the queue.

2 Likes

So what you’re saying is the playerEntered should be on the server?
If so, i dont think it can be like that

1 Like

That’s how I’d do it personally

1 Like

I’ll try to make it work on the server!

That didnt work, Its mainly because without a remote event its hard to get a player variable on the server

How does Zone work and when do you fire playerEntered?

Not sure LOL!
Its a module by ForeverHD
I think Im going to get rid of it and use something else

I looked at the documentation for ZonePlus which I assume is what you’re using right? The playerEntered callback takes a player argument. You should be able to use it on the server to get a reference to the player.

Better yet, they have a localPlayerEntered event. You can replace the playerEntered and playerExited calls with localPlayerEntered and localPlayerExited and it should work how you want.

I think you have to pass the player as a parameter for the functions, so it doesn’t fire on all clients

local player = game.Players.LocalPlayer
local Zone = require(game.ReplicatedStorage:WaitForChild("Assets").Modules.Zone)
local JoinQueue = game.ReplicatedStorage:WaitForChild("Remotes").JoinQueue
local ExitQueue = game.ReplicatedStorage:WaitForChild("Remotes").ExitQueue
local Model = workspace.MatchmakingZone
local zone = Zone.new(Model)

zone.playerEntered:Connect(function(enteredPlayer)
    if enteredPlayer == player then
        print(player.Name)
        JoinQueue:FireServer(player)
    end
end)

zone.playerExited:Connect(function(exitedPlayer)
    if exitedPlayer == player then
        print(player.Name)
        ExitQueue:FireServer(player)
    end
end)
1 Like

I imagine this is exactly what localPlayerEntered and localPlayerExited do internally?

1 Like

He needs to detect that there is at least 2 people in the queue which I don’t think localPlayerEntered is appropriate for this scenario since it only detect the local player and not a group of players

That’s fine since the client will fire to the server that they’ve entered the zone. The server is responsible for checking whether 2 or more players are in the queue and not the client.

1 Like

THANKS EVERY1 FOR THE HELP! im going to test the "local player entered " thing,
it should work

Actually i just went with making it a proximity prompt and that fixed it :person_shrugging:

1 Like

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