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

how do I make a line of code that randomly selects a team for you?

I made a script that if they dont choose a team on time then it picks a team for them. Here is what I got:

local ti = script.Parent.timeleft

function timeleft()
	for i = 10, 0, -1 do
		wait (1)
		ti.Text = "Time Left: " .. i ..""
		if i == 0 then
			ti.Text = "Times Up! Selecting Team For You..."
			local chooseteam = math.random(1,2)
			if chooseteam == 1 then
				-- Need Help with
			end
			if chooseteam == 2 then
				-- Need help with
			end
		end
	end
end


timeleft()

what should I do?

1 comment

0

In order to change the team a player is on, you need to change the TeamColor property of the player.

1 answer

✓ Accepted answer

First off, you are using "==" to compare your random numbers, which won't work, as the numbers generated are never actually whole. They way to do this is to check if the generated number is less than or greater than the midpoint of the range, which is usually 0-1, so a midpoint of 0.5. Secondly, you need a separate script on the server to actually assign teams. I've done that for you here:

In your localScript:

math.randomseed(tick()) --Make sure number is random
local replicatedStorage = game:GetService("ReplicatedStorage")

local ti = script.Parent.timeleft

function timeleft()
    for i = 10, 0, -1 do
        wait (1)
        ti.Text = "Time Left: " .. i ..""
        if i == 0 then
            ti.Text = "Times Up! Selecting Team For You..."
            local chooseteam = math.random()
            if chooseteam <= .5 then
                replicatedStorage:WaitForChild("PickTeam"):FireServer("Blue Team")
            elseif chooseteam >= .5 then
                replicatedStorage:WaitForChild("PickTeam"):FireServer("Red Team")
            end
        end
    end
end

timeleft()

In a server script:

--Create your teams--
local replicatedStorage = game:GetService("ReplicatedStorage")
local pickTeamEvent = Instance.new("RemoteEvent", replicatedStorage)
pickTeamEvent.Name = "PickTeam"

local pickingTeam = Instance.new("Team", game:GetService("Teams"))
pickingTeam.TeamColor = BrickColor.new("Dark stone grey")
pickingTeam.Name = "Picking Team"


local redTeam = Instance.new("Team", game:GetService("Teams"))
redTeam.TeamColor = BrickColor.new("Really red")
redTeam.Name = "Red Team"

local blueTeam = Instance.new("Team", game:GetService("Teams"))
blueTeam.TeamColor = BrickColor.new("Really blue")
blueTeam.Name = "Blue Team"

--Assign given team to player
function AssignTeam(player, team)
	if team == "Red Team" then
		player.Team = redTeam
	elseif team == "Blue Team" then
		player.Team = blueTeam
	end
	print("Assigned Team!")
end
pickTeamEvent.OnServerEvent:connect(AssignTeam) --RemoteEvents automatically provide the player that fired as their first argument.

What happens here is that while the localscript is counting down from 10, the server creates a RemoteEvent which takes the player, and team you want to assign them to and does it. You only want to create and modify things in a server script, to work with FilteringEnabled. When the client's team is chosen in the localScript, this event is fired and the server assigns the player to one of the created teams. Simple!

If I've helped you, make sure to mark my answer correct :)

1 comment

0

Its works. And I also get it too! Thanks!

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