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

Help With Randomizing Spawns?

Asked by
Scootakip 299 Moderation Voter
8 years ago
function ToonsToBattle()
    local num = math.random(3)
    for i,player in pairs(game.Players:GetPlayers())do
    local torso=player.Character:FindFirstChild("Torso")
    if torso then
    if player.TeamColor == BrickColor.new("Bright red") then
        if num == 1 then
            torso.CFrame=TS1+Vector3.new(0,i*5,0)
        elseif num == 2 then
            torso.CFrame=TS2+Vector3.new(0,i*5,0)
        elseif num == 3 then
            torso.CFrame=TS3+Vector3.new(0,i*5,0)
        end

end
    end
end
end

This script is a function that when called, we take everyone on the Bright red team and move each player to one of three spawns. What I want it to do is select a random spawn for each team member, but instead it selects one for all of them to be teleported to. I thought that making the value of 'num' local would have each player have it happen individually, but that didn't work. Some help with this?

1 answer

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
8 years ago

The purpose of a for loop is to do a block for each thing.

You don't define num inside the for loop, so it's not done for each.

If you want to do it for each, put it inside the loop.


Tab your code correctly!


Don't repeat yourself! If you find yourself writing code with variables like TS1, TS2, TS3, that's usually a sign you should be using a list instead.

local num = math.random(#Spawns)
torso.CFrame = Spawns[num] + Vector3.new(0, i * 5, 0)
0
what is spawns TheTermaninator 45 — 8y
0
Spawns is a list of Vector3s that you want players to spawn at. BlueTaslem 18071 — 8y
Ad

Answer this question