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

My script works for the first player who joins, but doesn't for the second player?

Asked by 6 years ago
Edited 6 years ago

Notes;

  1. This is Normal script

  2. This is in server script service

here is my code;

local teamForFirstGuy
local BlueSpawn = game.Workspace:WaitForChild('BlueTower').BlueSpawn
local RedSpawn = game.Workspace:WaitForChild('RedTower').RedSpawn
local AOP = game.ReplicatedStorage:WaitForChild('AmountOfPlayers')
local BlueEffect = game.Lighting:WaitForChild('Blur')
local W = 0.07

local function AddPlayer(plr)
    AOP.Value = AOP.Value + 1

    local Stats = Instance.new('Model')
    Stats.Name = 'PlrStats'
    Stats.Parent = plr


    local Team = Instance.new("StringValue")
    Team.Name = "Team"

    if teamForFirstGuy then
        Team.Value = teamForFirstGuy == "Blue" and "Red" or "Blue"
    else
        Team.Value = math.random() < .5 and "Blue" or "Red"
        teamForFirstGuy = Team.Value

        Team.Parent = Stats

        plr.CharacterAdded:Connect(function(char)
               if Team.Value == 'Red' then
                wait()
                char:WaitForChild('HumanoidRootPart').CFrame = BlueSpawn.CFrame
            else
                wait()
                char:WaitForChild('HumanoidRootPart').CFrame = RedSpawn.CFrame
            end
        end)    
    end
end
game.Players.PlayerAdded:Connect(function(player)
    AddPlayer(player)
end)

The problem is probably here;

if teamForFirstGuy then
        Team.Value = teamForFirstGuy == "Blue" and "Red" or "Blue"
    else
        Team.Value = math.random() < .5 and "Blue" or "Red"
        teamForFirstGuy = Team.Value

        Team.Parent = Stats

        plr.CharacterAdded:Connect(function(char)
               if Team.Value == 'Red' then
                wait()
                char:WaitForChild('HumanoidRootPart').CFrame = BlueSpawn.CFrame
            else
                wait()
                char:WaitForChild('HumanoidRootPart').CFrame = RedSpawn.CFrame

Because the first player gets's teleported to the correct spawn, the second player spawns in ROBLOX default spawn. So maybe I gotta change it so that if the player is on team blue then the next player is on team red? Please help!

Thank you for any support, feed back and help!

Answer this question