I’ve thought of this role-choosing-thing system, and in my mind, it will take all the players in game, and give them a random role. But, some roles are necessary to play the game and I need them to be required somehow, and I need some roles to be limited in number/more rare or common.
Is there anyone that knows how I would start on a system like this??
You could use tables to do what you is saying.
Exemple:
local Roles = {
Police = {
Rarity = 4,
Players = 0 -- the number represents the number of players with this role
},
Criminal = {
Rarity = 4,
Players = 0
},
Other = {
Rarity = 15,
Players = 0
}
}
local function checkRolePlayers(role, minPlayers, func)
if Roles[role].Players < minPlayers then
func()
end
end
checkRolePlayers("Criminal", 4, function()
-- do something
end)
checkRolePlayers("Police", 4, function()
-- do something
end)
checkRolePlayers("Other", 4, function()
-- do something
end)
Thank you, but is there a way to make some roles required, like there will always be one of these no matter what? (min players is 2 if u need that info)
edit: didnt realize it covers it kinda, oops
You already have the right idea for the required roles, you just gotta loop through all the players and randomly assign them to however many players need them. If you’re wondering how to keep track of number of players who have one of the required roles, you could create an integer variable that increments after a player is given a role. The other roles can then be added after through a rarity system similar to what @miguel555555ft did.
can you implement this kinda on the go rn
Sure thing. This is a pretty simple way of how I’d go about assigning the required roles. The string value I added in represents their role.
local players = game:GetService("Players")
local requiredRoles = {"Role #1", "Role #2"}
local function gameStarted()
local getPlayers = players:GetPlayers()
local numOfPlrsWithRequiredRole = 0 -- the incrementation I was talking about
if #getPlayers < #requiredRoles then -- this is just a check to see if there are enough players in the game
return
end
for i, role in ipairs(requiredRoles) do
local randomPlayer
repeat
randomPlayer = getPlayers[math.random(1, #getPlayers)]
until not randomPlayer.role or randomPlayer.role.Value == ""
randomPlayer.role.Value = requiredRoles[i]
numOfPlrsWithRequiredRole += 1
print(randomPlayer.Name .. ": " .. randomPlayer.role.Value)
end
end
players.PlayerAdded:Connect(function(player)
local role = Instance.new("StringValue", player)
role.Name = "role"
gameStarted() -- put this function wherever it needs to go, I put it here for testing purposes.
end)
do i need a new numOfPlrsWithRequiredRole for each required role?
You would only need a new numOfPlrsWithRequiredRole when a new game is starting. It should be useful for assigning the other roles.
i dont want to be asking too much, just let me know if you cant put the full system into this script i made before
or just add a section for the unrequired roles in your code
theres two minimum players and two required roles for the game
local values = game.ServerStorage.GameValues
local status = values.Status
local stolen = values.MoneyStolen
local minPlayers = 2
local interTime = 16
while task.wait() do
repeat
task.wait(1)
interTime = interTime - 1
status.Value = "Intermission: "..interTime.." seconds left"
until interTime == 0
interTime = 15
repeat
task.wait()
status.Value = "Waiting for a minimum of "..minPlayers.." players"
until #game.Players:GetPlayers() >= minPlayers
status.Value = "Preparing map.."
--TODO: add map thing
task.wait(0.7)
status.Value = "Choosing roles.."
--this is where the roles are chosen
end
Not a problem, I’ve also added the other roles. Let me know if it works out.
local players = game:GetService("Players")
local values = game.ServerStorage.GameValues
local status = values.Status
local stolen = values.MoneyStolen
local minPlayers = 2
local interTime = 16
local requiredRoles = {"Role #1", "Role #2"}
local otherRoles = {"OtherRole #1", "OtherRole #2"}
players.PlayerAdded:Connect(function(player)
local role = Instance.new("StringValue", player)
role.Name = "role"
end)
local function chooseRoles()
local getPlayers = players:GetPlayers()
local playersWithOtherRoles = {}
local numOfPlrsWithRequiredRole = 0
if #getPlayers < #requiredRoles then
return
end
for i, role in ipairs(requiredRoles) do
local randomPlayer
repeat
randomPlayer = getPlayers[math.random(1, #getPlayers)]
until not randomPlayer.role or randomPlayer.role.Value == ""
randomPlayer.role.Value = requiredRoles[i]
numOfPlrsWithRequiredRole += 1
print(randomPlayer.Name .. ": " .. randomPlayer.role.Value)
end
for i, player in pairs(players:GetChildren()) do
local alreadyHasRole = false
for j, role in ipairs(requiredRoles) do
if player.role.Value == requiredRoles[j] then
alreadyHasRole = true
end
end
if not alreadyHasRole then
table.insert(playersWithOtherRoles, player.Name)
end
end
for i, checkRole in ipairs(playersWithOtherRoles) do
local player = players:FindFirstChild(checkRole)
if player then
local randomOtherRole = math.random(1, #otherRoles)
player.role.Value = otherRoles[randomOtherRole]
print(player.Name.. ": ".. player.role.Value)
end
end
end
while task.wait() do
repeat
task.wait(1)
interTime = interTime - 1
status.Value = "Intermission: "..interTime.." seconds left"
until interTime == 0
interTime = 15
repeat
task.wait()
status.Value = "Waiting for a minimum of "..minPlayers.." players"
until #game.Players:GetPlayers() >= minPlayers
status.Value = "Preparing map.."
--TODO: add map thing
task.wait(0.7)
status.Value = "Choosing roles.."
for i, player in pairs(players:GetChildren()) do
player.role.Value = ""
end
chooseRoles()
end
EDIT: I almost forgot to reset the string values before the roles were chosen 