So am make a random team generator that place players in the server into random teams but i keep having an issue.
Local Script
local rs = game.ReplicatedStorage.RedAndBlueEvent
local button = script.Parent
button.MouseButton1Click:Connect(function()
rs:FireServer()
end)
ServerScript
local rs = game.ReplicatedStorage.RedAndBlueEvent
local trainee = game.Teams.Trainee
local red = game.Teams.Red
local blue = game.Teams.Blue
local teams = {
red,
blue
}
rs.OnServerEvent:Connect(function(player)
for i, v in pairs(trainee:GetPlayers()) do
local randomTeam = teams[math.random(1, #teams)]
v.Team = randomTeam
end
end)
rs.OnServerEvent:Connect(function(player)
for i, v in pairs(trainee:GetPlayers()) do
local randomTeam = teams[math.random(1, #teams)]
v.Team = teams[randomTeam]
end
end)
math.random() - thefunction that return the random only number
why means ‘teams[randomTeam]’?
well, because when you create a table, the server distributes them in turn, for example:
The original script looked just fine. You indexed the table with the number that the math.random function returned, so the variable should’ve been a team, not a number. The original code should’ve worked.
The teams already have numbers assigned to them as they’re in a table, by default teams[1] will be red as it’s first in the table, and teams[2] will be blue as it’s second in the table.
local red = game.Teams.Red
local blue = game.Teams.Blue
local teams = {
red,
blue
}
print(teams[1])
print(teams[2])
Doing this you’d get an output of, without having to add anything extra to your table.
The above would be the same as: (assuming by chance the random numbers are 1 and 2 respectively)
Not quite sure why it wasn’t working for you in the first place as the code you originally had works perfectly, just tested it myself in studio. Glad it’s working for you now either way.
I think current solution is more convenient, besides, I understand what you mean, but it will be some kind of long code if I try to do so instead of your solution:
rs.OnServerEvent:Connect(function(player)
for i, v in pairs(trainee:GetPlayers()) do
local randomTeam = teams[math.random(1, #teams)]
if randomTeam = 1 then
player.Team="red"
elseif randomTeam = 2 then
player.Team="blue"
end
end
end)
You wouldn’t need to do this as they are already ordered in the table so red = 1 and blue = 2, keep in mind that the randomTeam variable isn’t giving you a number, it’s giving you the team.
teams = {red, blue}
local randomTeam = teams[math.random(1, #teams)]
player.Team = randomTeam
If the random number was 1, they would be placed on red and if it were 2 they’d be placed on blue
This is the most convenient way to do it, feel free to test in studio if you wish.
rs.OnServerEvent:Connect(function(player)
for i, v in pairs(trainee:GetPlayers()) do
local randomTeam = teams[math.random(1, #teams)]
v.Team = randomTeam
end
end)
I mean this in the most respectful way possible, but this won’t work at all.
You’re indexing the teams table with the number that was randomly selected and are setting the randomTeam variable to whatever is returned, so the value of that variable is going to be a team. All you have to do after the first line is just set the player’s team to the variable.
The conditional statement is futile and wouldn’t work anyways; the randomTeam variable is a team so neither of the two conditions would be met, and you can’t set a player’s team to a string. You’re also supposed to use two equal signs in conditionals, not one.