Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Script Wont Assign Teams To Players? | SOLVED

The script is suppose to run through the intermission time, then once it has completed, assign teams randomly to the players, and start the round timer, but this does not happen, the timers run, but teams do not get assigned.

--Round Variables
local timeint = 10 --Numbers read as seconds
local inttime = 10 --Numbers read as seconds
local requiredplayers = 2
--Round Objects
local status = game.ReplicatedStorage.Status
local teams = {"Red","Blue"}
--Assign Teams
function Assign()

    local players = game.Players:GetPlayers()

    for i = 1,#players do
        local ChosenTeam = teams[math.random(1,#teams)]
        players[i].TeamColor = game.Teams:FindFirstChild(ChosenTeam).TeamColor
    end
end
--Main
while wait() do
	for i=inttime,1,-1 do
		wait(1)
		status.Value = "Intermission: "..i
	end
	for i=timeint,1,-1 do
		wait(1)
		Assign()
		status.Value = "Time Left: "..i
	end
end

Thanks!

1 comment

0

Not sure if this is the answer but one thing I noticed is that Assign() is called 10 times because it is in the for loop. Not sure if this is intended or not.

1 answer

✓ Accepted answer

There is now a property called Team, so you should use that instead of TeamColor. Also, try adding a Team object in Teams and then use :GetTeams() because it will return ALL the teams. Apart from that, calling the Assign() function in a for loop will do so multiple times... so be careful where your line of code is in. Plus, you want to assign the teams once, right?

So, let's fix the script for you!

--Round Variables
local timeint = 10 --Numbers read as seconds
local inttime = 10 --Numbers read as seconds
local requiredplayers = 2
--Round Objects
local status = game.ReplicatedStorage.Status
local teams = game:GetService("Teams"):GetTeams() -- Gets all the teams.
--Assign Teams
function Assign()

    local players = game.Players:GetPlayers()

    for _, player in pairs(players) do
        local ChosenTeam = teams[math.random(1,#teams)]
        player.Team = ChosenTeam -- Use the Team property.
    end
end

--Main
while wait() do
    for i=inttime,1,-1 do
        status.Value = "Intermission: "..i
        wait(1)
    end
    Assign()
    for i=timeint,1,-1 do
        status.Value = "Time Left: "..i
        wait(1)
    end
end

Any questions? Leave them in the comments. Thanks! :)

5 comments

0

Yeah, someone helped me figure that out a few minutes ago

0

XD

0

I tried to add a auto balance element, but that broke it again,

0

hey lemme help you

0

use :GetPlayers() for a team to return the players who are in that team

This discussion is preserved as it was. New answers, comments, and votes are closed.