Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 6 years ago

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?

0
In order to change the team a player is on, you need to change the TeamColor property of the player. awfulszn 394 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago

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 :)

0
Its works. And I also get it too! Thanks! Mohawkid14 28 — 6y
Ad

Answer this question