I could make it so:
Table = {}
Table2 = {}
Ta…
But i want it different, in my Game they can create a Match and others can join them. How could i create a table?
I could make it so:
Table = {}
Table2 = {}
Ta…
But i want it different, in my Game they can create a Match and others can join them. How could i create a table?
If I’m understanding the question right, you want to allocate a new table dynamically? You could use subtables within a table acting as the master container. Your example of a match might look like:
local match = {
players = {
}
}
and you could call table.insert(match.players, Player)
on a player instance.
If you wanted to add something new to match you’d just do math.thing = {}
to add a subtable thing inside match.
Thanks and how could i name the Table? I mean if the Players create a Table and other Players want to join the Player, i send a Remotefunction to the Server with the Players name who created the Table how could i find the Table?
Or i could give it just a random Name.
Might look something like this:
local matches = {}
server receives a remoteevent from a client – they’re creating a new party, now:
matches[tostring(Player.UserId)] = {}
This makes matches:
local matches = {
"1234" = {}
}
Another player sends a remote event they want to join that person, so they fire the ID of the person to join to the server:
if table.find(matches, playerToJoinID) then [...]
if playerToJoinID exists, it’s a valid table in matches, now the server can do:
table.insert(matches[playerToJoinId], playerJoiningThemId)
now matches may look like:
local matches = {
"1234" = {
"5678"
}
}
So then you know user with id 5678 has joined the party of a user with id 1234. Now maybe user 1234 says they want to close the party, the server can do:
matches[player's ID] = nil
to remove that subtable, or alternatively
table.remove(matches, player's id)
Thank you so much i will try this now! And do i teleport with
teleportService:TeleportToPrivateServer(PlaceId,reserveServerNumber,playerToJoinId) ?
You’d need to convert that table of ID’s into a table of Player instances, which might look like:
local matches = {
"1234" = {
"5678"
}
}
local party = {}
--let playerToJoinId be the string "1234"
--we're getting everyone in 1234's party
for _, player in pairs(game:GetService("Players"):GetPlayers()) do--look through all connected players
if table.find(matches[playerToJoinId], tostring(player.UserId)) then--if their ID is in the party...
table.insert(party, player)--put them in the new container for Player instances
end
end
teleportService:TeleportToPrivateServer(PlaceId, reserveServerNumber, party)--send the party
table.remove(matches, playerToJoinId)--remove the party from the list now that they're gone
table.clear(party) --safe way to cleanup a temporary table
Since the owner of the party is being used as the key and not a value in your subtable, be sure to add them to their own party so that they’re a member of the group being sent. That would look like
table.insert(matches[playerToJoinId], playerToJoinId)
*I should also mention the reason behind using UserId’s as the method of tracking people is that it’s the safest way you can reference someone. With this method, if they disconnect, they’re ignored in the loop forming the party of players that’s sent. If you were holding a reference to a player in matches and said person disconnects, their reference becomes bad and may error/cause problems for others.
Thank you so much! I will try it now
I have one Question about the Party Table, if 2 Players who created a Match starting the Game they both will add to the Party Table would they both not teleported to the same Private Server? Or i am wrong?
Two matches as in two parties like this?
local matches = {
"1234" = {
"5678"
},
"9012" = {
"3456"
}
}
If you send the party from matches[“9012”] then no one outside of it will get sent, after they leave it will be back to just the party of “1234” remaining
Do i need the
for _, player in pairs(game:GetService(“Players”):GetPlayers()) do
to check if the Player has already a table or players already joined to, prevent them to join or create a match again?
Or could i not just made
if not table.find(matches[playerToJoinId], tostring(player.UserId))
I tried this:
if not table.find(gameMatches, player.UserId) then
gameMatches[tostring(player.UserId)] = {}
But its not working “if i maked everything right”, they dont find the Player in the Table.
table.find
attempts to find only the specified value in the table, which is not actually the player’s user id. (it’s the key, or index which points to a value, in this case, the table object.)
You can index the party table by using the key directly, like so:
--//table.find only finds the specified value, in your case, you were trying to find the key/index that points to the value.
local match = gameMatches[tostring(player.UserId)]
if match then
--//match is found
end
I have one last question sorry for that much question,
how can i check if the player who wants to join or want to create a game is not already in a match what someone else created?
Would this work?
local match = matches[tostring(player.UserId)[tostring(player.UserId)]]
if not match then
end
You could create a linear search function to check each match table to see if the specified player is in there.
local function isPlayerInLobby(playerToFind)
for _, lobby in pairs(matches) do
for _, player in ipairs(lobby) do
if player == playerToFind then
return true
end
end
end
end
I will also insert Variables where i put numbers, in the Lobby Table do i need the tostring?
How could i print out the Joined Player and make the Joined Player nil?