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)
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.
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)
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.