i need some help coming up with how i could make code for an omegle-type one on one chat game for roblox, essentially how i think it could be done is
add players to queue (if they join, if they left a chat, etc.) using tables for a queue
if there is > 2 players in the queue, the first 2 will be put in a room
send a remote event to both clients to setup a room (not sure how i would make the rooms work tho)
if one disconnects, the gui gets destroyed and they both go back to queue
i have a gui already but i have almost no scripts other than simple stuff like limiting the characters that can be typed in a textbox etc. what i need help with doing is:
making the queue system
making the room system (storing who is in a room together if needed )
making the disconnect event
the game is fully gui based so i cant used teleport service or anything, i am not asking for scripts to be made for me all i need is explanation on how it could be done, and if you want i can use my own brain to make the actual script thank you all, sorry if this is a lot to ask for but any help is appreciated!
(hope this is okay to ask in here lol, havent had luck anywhere so thought id try the dev forums)
The essence of making something like this isn’t actually that hard, but putting it into effect is a bit different.
Most of the logic and processing will be performed on the server; the client only reponds to updates with the queue, joining a room, sending a message etc.
This gives an outline of what the server would do to add players to a room:
local Players = game:GetService('Players')
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local queue = {}
local rooms = {}
Players.PlayedAdded:Connect(function(player)
-- Confirm that player is not already in the queue (if they are, remove and readd them)
local pos = table.find(queue, player.UserId)
if pos ~= nil then table.remove(queue, pos) end
table.insert(queue, player.UserId) -- Add player to queue at the end (last in queue)
end)
Players.PlayerRemoving:Connect(function(player)
-- Remove player from queue if they are in it
local pos = table.find(queue, player.UserId)
if pos ~= nil then table.remove(queue, pos) end
-- Process player leaving room
for roomId, data in ipairs(rooms) do
if data.user1 == player.UserId then
ReplicatedStorage.PlayerLeftRoom:FireClient(data.user2)
table.remove(rooms, roomId)
table.insert(queue, data.user2)
elseif data.user2 == player.UserId then
ReplicatedStorage.PlayerLeftRoom:FireClient(data.user1)
table.remove(rooms, roomId)
table.insert(queue, data.user1)
end
end
end)
while true do
if #queue >= 2 then
local player1 = Players:GetPlayerFromUserId(queue[1])
local player2 = Players:GetPlayerFromUserId(queue[2])
local roomId = rooms[#rooms + 1]
-- Add both players to a room
table.insert(rooms, roomId, {
user1 = player1.UserId,
user2 = player2.UserId
})
-- Remove players from queue
table.remove(queue, 1)
table.remove(queue, 2)
-- Fire remote events to inform clients that they have been added to a queue, providing the roomId parameter so that they know what to send back to the server
ReplicatedStorage.JoinRoom:FireClient(player1, roomId)
ReplicatedStorage.JoinRoom:FireClient(player2, roomId)
end
task.wait()
end
I hope this gives you an insight into the process you would need to follow to get this working. Please feel free to DM me if you would like further assistance.
thank you so much! this works perfectly, needed to do alot of tweaking and im currently adding alot to fit my needs, but this worked perfectly as a template for my queue and room system.
had a few issues and realised it was because of the while true do, so i changed it to runservice heartbeat and it works flawlessly, also with the table.remove(queue, 1) table.remove(queue, 2)
i had to change both of the positions to 1, otherwise it was removing the wrong players
also thanks to this i know how to use dictionary’s and tables a whole lot better , so thank you so so so much.