I’m trying to make a 1v1 matchmaking system that waits until there’s an available player in the queue, after one is found it removes both players from the queue and creates a table with both players in it.
I set it so after the table was created, it printed out who was fighting in this format: “Player1 VS Player2”. When I test it out I see that the players are 1v1ing themselves as shown in this screenshot:
I think this might have to do with me improperly referencing both players but, when I review my code I can’t see how this is possible
local function OnRequest(player)
print("recieved ".. player.Name)
local IsQueued = player.IsQueued
local Teamate = player.Teamate
-- checks if player is eligable to be queued
if not Teamate.Value and IsQueued.Value == false then
table.insert(Searching,1,player)
IsQueued.Value = true
-- iterates through searching table until it finds a plr then creates a table with both players
RunService.Stepped:Connect(function()
for i, plr in pairs(Searching) do
print(plr.Name.." is searching")
if plr and plr.Name ~= player.Name then
table.remove(Searching,i,plr,player)
local Matchup = table.create(2,player, plr)
print("Matchup: ".. Matchup[1].Name.." VS "..Matchup[2].Name)
--TPs both players
Matchup[1].Character.HumanoidRootPart.CFrame = game.Workspace.Part.CFrame
Matchup[2].Character.HumanoidRootPart.CFrame = game.Workspace.Part.CFrame
--deletes table
Matchup = nil
return
end
end
end)
You’re doing Matchup[1] which is getting the first value in the table which is 2, Then you’re doing Matchup[2] in the table the second value is player.
The player is kinda random because you’re just saying player not plr. Try change that.
local MatchMakingTable = {
["1Player"] = player
["2Player"] = plr
}
If this doesn’t work then its because its most likely choosing the wrong player in thre [“1Player”] argument because player is a random player since you’re not refrencing it to a playername.
What you could do is print out the plr name and if 1Player is chosen and its the same name as 2Player then you could change it to something else.
There are two things here that are a bit wonky with your code, one of which is the issue you’re actually trying to solve.
Reading linearly, I first saw this: table.remove(Searching,i,plr,player), an incorrect usage of table.remove. table.remove takes two parameters: the table to remove from & the index in the table to remove; it shifts the following indices down to fill in the gap. In this circumstance, you’re only removing plr from your table and not player. Be careful.
Your legitimate issue is your use of table.create. If you read the DevHub docs, (table | Documentation - Roblox Creator Hub – will need to scroll a tidbit to find create), it takes two parameters: a number, the amount of indices you want to create, and a Variant (one) which each key will be filled with. In this case, you’re creating a table { player, player } which is why it keeps matching the target user up against themselves.
You have two options here.
With table.create:
local Matchup = table.create(2)
Matchup[1] = player
Matchup[2] = plr
Without table.create:
local Matchup = { player, plr }
Hopefully this solves your issue. Be sure to check API docs when using these sorts of methods!