Make players assigned to random rooms?

This is a simple resource request, I do not need help with the script itself. I truly do not know where I should begin.

What should I be looking into in order to create this? I’ve guessed tables but it’s likely much more than that and I don’t know where to begin, this is my first time making something like this.

The idea is simple, when a player joins it will assign two players to different rooms and there will be roughly 20 rooms, these rooms will serve the purpose as being able to be decorated and such, though that’s not relevant here.

I have figured that the best result would be using tables to store the players for each room as it being handled inside the script would be better for anti-exploits, rather than having it inside a string which is a child of each room and will contain the two player names. The primary issue that I see arising with this script is how I’m going to make it so the table recognises each room as being different and is able to accurate add players upon join and remove them upon join from the correct rooms.

I don’t exactly know the best way to describe this so if it wasn’t clear enough I can give better clarification.

1 Like

Hey! I do understand the way you want to implement your code. However, I have another solution in mind which can help you be more organized and make your idea easy to implement.

I suggest that you make use of folders and StringValues. Here, you place the folders maybe under ServerStorage and use it to name the rooms (e.g Room1, Room2). Then, for each folder, use StringValues to name the players present in that room, otherwise give it a value of None. In your script, you just have to listen for a player join and player leave event, and just do the necessary actions.

Since the data is stored in ServerStorage, this will not be accessible by exploiters and hence will be unexploitable. This is different from your initial solution since I believe the strings under the rooms are in Workspace.

This is just a suggestion though. You may still opt for different ways but I hope this helps!

1 Like

I think a good start would be some research on Random.new(). I prefer this to math.random() as its results seem, for lack of a better term, more random.

I recommend you look into for loops such as for, i,v in pairs which can run through all children within a certain context and perform certain actions on them.

If you’re looking to assign each player a random room out of the 20, you can choose a player to assign first and run a Random.new() function which picks a number between 1 and 20. After that number is returned, remove that number from the possibility of being chosen and run the same Random function for the second player. You’ll have two random numbers between 1 and 20 which you can use to assign to each player respectively.

1 Like

@AgencyDrone
(of course all of this will be handled in the obligatory PlayerJoin and PlayerLeave)
Wish I could mark two as a solution. Serverstorage with stringvalues seems like a good alternative to tables(which frankly from my current knowledge of them seemed impossible to do this for without getting messy).
So far after messing around with your idea as well as Agency drone’s I’ve come to a conclusion of:

Looping through the folder in ServerStorage which I named “Rooms” and then picking a random folder from one of the 20 folders called “Room1”, “Room2” etc through using math.random() in correlation with :NextInteger(1, #Children) so it won’t go over the cell limit(Prior to this I made a children value called children = ServerStorage.Rooms:GetChildren())

Then converting the randomly assigned 1-20 value to a string and searching through substrings for the folder with the name that correlates with the assigned value. For example, if the number thrown up from NextInteger was 10 it would search for “Room10”, it’ll then check to see if one of the strings in that folder isn’t nil and if it isn’t nil it’ll edit the non-nil one to match the player name and it’ll do this every time a player joins. The rooms names will match the names of the models in workspace so the script knows the correlation between the room folder in serverstorage and the room in workspace…

Additionally, when the player leaves it will search for the player in the folders and remove them from it.
Also, the spawn script which will respawn them in the room will search for them in serverstorage as well so it knows what room to place them in.

Does this seem ineffective in any way? Here’s what I have so far as a reference:

Please note, the reference is just me messing around with math.random and loops to try and make them work well together. This is not a part of the script which will assign rooms.

local CellAssign = Random.new()

local ServerStorage = game:GetService("ServerStorage")

local children = ServerStorage.Rooms:GetChildren()

local x = 0

for i = 1, #children do

wait()

local CellChosen = CellAssign:NextInteger(1, #children)

local CellConversion = tostring(CellChosen)

print("Cell: "..CellConversion)

end
1 Like

The following does this:

  1. Insert a couple of room objects into a table.
  2. Each room object has a room number, spawn position, and a property for storing a player
  3. A player is inserted into a random room when they join
  4. After a couple of seconds, the player is teleported to the room position.
  5. Using a room object makes it easier for you, because you can ensure that the room is only occupied by a player. (if roomForPlayer.Player is nil, then it is an empty room)
  6. You can store other properties for the room as well.

Insert this script into ServerScriptService:

local Players = game:GetService("Players")
local RoomObject = require(script:WaitForChild("RoomObject"))

local availibleRooms = {}




--change the room spawn position by changing the value in Vector3.new()
--you can add as many rooms as possible, just change number and position of the room.

table.insert(availibleRooms, RoomObject.new(1,Vector3.new(50,0,0),nil))
table.insert(availibleRooms, RoomObject.new(2,Vector3.new(100,0,0),nil))
table.insert(availibleRooms, RoomObject.new(3,Vector3.new(150,0,0),nil))
table.insert(availibleRooms, RoomObject.new(4,Vector3.new(200,0,0),nil))
table.insert(availibleRooms, RoomObject.new(5,Vector3.new(250,0,0),nil))




Players.PlayerAdded:Connect(function(player)


	
	if #availibleRooms ~= 0 then
		local randomRoomNumber = math.random(1,#availibleRooms)
		local roomForPlayer = availibleRooms[randomRoomNumber]
		roomForPlayer.Player = player
		print(availibleRooms, randomRoomNumber)
		availibleRooms[randomRoomNumber] = nil
		
		
		wait(3)
		local Character = player.Character or player.CharacterAdded:Wait()
		Character.HumanoidRootPart.CFrame = (CFrame.new(roomForPlayer.spawnPosition + Vector3.new(0,1,0)))
		print(player.Name .. " joined the game, and has been assigned room number ", randomRoomNumber)
	else
		print("There are no more rooms remaining.")
	end

end)
local randomRoom

And then insert this as a module script inside of the script above.

local module = {}
local module_mt = {__index = module};


function module.new(roomNumber, spawnPosition,Player)
	local self = {}
	self.roomNumber = roomNumber
	self.spawnPosition = spawnPosition
	self.Player = Player
	
	return setmetatable(self, module_mt);
end


return module

You can use tables if you use utilize the table.remove() method. It can specifically remove the specific item that has already been randomly drawn. After that you can then run another Random.New() with the limit being #tablevariablename.

It’s late(or ‘early’ if you actually slept) and my English is about to become the furthest thing from comprehensible, do you mind if I return to this later? I have a question about how to handle this with removing the option from tables and it’s best I do it when my mind isn’t slush. Alternatively, could I open a DM with you about this that I can return to later? The first option is acceptable if not though the second option is the most handy for me.

I’d be more than happy to help through DMs, although I’m hitting the hay right now!

1 Like