How can I make a script auto assign roles to somebody

I have a script in my roblox game that basically picks a RANDOM player from the game to try and assign roles to a player.

Here are the assignable roles.

local InLobby = {}
local hiders = {}
local seeker = ""

And I want it to pick a random person in the roblox game to become either a seeker or a hider. I tried math.random.

Also just a note. I only want ONE seeker and the rest of the players will be hiders.

1 Like

Try making a table of all the players and choose a random player as follows

Idk how your sistem works, but implementing this base will be enough.
Optionally you can use math.randomseed(os.time())

-- I suppose the players will be in the lobby by default
for index, player in pairs(game.Players:GetPlayers()) do
	table.insert(InLobby, player.Name)
end

-- Choosing random seeker
seeker = InLobby[math.random(1, #InLobby)]

-- The rest are hiders
for index, name in pairs(InLobby) do
	if name ~= seeker then -- seeker can't be hider
		table.insert(hiders, name)
	end
end

-- Players no longer in lobby
table.clear(InLobby)
1 Like

You could make use of Team’s and use that to organise what player is what. :slight_smile:

-- Get our services here.
local Teams = game:GetService("Teams")
local Players = game:GetService("Players")

-- Find/locate the teams, if they don't exist, make them.
local lobbyTeam: Team = Teams:FindFirstChild("Lobby", 2)
if not lobbyTeam then
	lobbyTeam = Instance.new("Team")
	lobbyTeam.Name = "Lobby"
	lobbyTeam.TeamColor = Color3.fromRGB(160,160,160)
	lobbyTeam.AutoAssignable = true
	lobbyTeam.Parent = Teams
end

local hidersTeam: Team = Teams:FindFirstChild("Hiders")
if not hidersTeam then
	hidersTeam = Instance.new("Team")
	hidersTeam.Name = "Hiders"
	hidersTeam.TeamColor = Color3.fromRGB(85, 189, 149)
	lobbyTeam.AutoAssignable = false
	lobbyTeam.Parent = Teams
end

local seekersTeam: Team = Teams:FindFirstChild("Seekers")
if not seekersTeam then
	seekersTeam = Instance.new("Team")
	seekersTeam.Name = "Seekers"
	seekersTeam.TeamColor = Color3.fromRGB(200, 106, 107)
	seekersTeam.AutoAssignable = false
	seekersTeam.Parent = Teams
end
	
-- This is the method you'd call when a game is about to start.
function SetupHideAndSeek()
	local lobbyPlayerList = lobbyTeam:GetPlayers()
	
	-- Generate a random number to index lobbyPlayerList with.
	local seekerChosenNum: number = Random.new():NextInteger(1, #lobbyPlayerList)
	
	-- Set the chosen player's team to the seekers, removing them from the list of players in the lobby as well.
	local chosenPlayer: Player = table.remove(lobbyPlayerList, seekerChosenNum)
	chosenPlayer.Team = seekersTeam
	
	-- Iterate through the remaining players and set their team to be hiders.
	for _, player: Player in ipairs(lobbyPlayerList) do
		player.Team = hidersTeam
	end
end

-- This is the method you'd call once the game has ended.
function HideAndSeekGameFinished()
	
	-- Set all players to be within the lobby team, since that's the default anyway regardless.
	for _, player: Player in ipairs(Players:GetPlayers()) do
		player.Team = lobbyTeam
	end
end

-- This is the method you'd call when someone becomes a seeker.
function SetPlayerAsSeeker(player: Player)
	player.Team = seekersTeam
end

You could also use tables and what-not to organise the seekers from the hiders etc, but Teams makes it easier.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.