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

My spawn function works in studio but doesn't work in game?

Asked by 3 years ago
local roundLength = 60
local intermissionLength = 20
local inRound = game.ReplicatedStorage.InRound
local teams = game:GetService('Teams')
local status = game.ReplicatedStorage.Status
local blueSpawn = game.Workspace.Map.BlueSpawn
local redSpawn = game.Workspace.Map.RedSpawn
local lobbySpawn = game.Workspace.Lobby.LobbySpawn
local redWin = game.ReplicatedStorage.RedWin
local blueWin = game.ReplicatedStorage.BlueWin
local respawnTowers = game.Workspace:WaitForChild('RespawnTowers')

game:GetService('Players').PlayerAdded:Connect(function(plr)
    plr.CharacterAdded:Connect(function(character)
        character:WaitForChild('Humanoid').Died:Connect(function()
            plr.Team = teams['Lobby']
        end)
    end)
end)

inRound.Changed:Connect(function()
    wait(1)
    if inRound.Value == true then
        for _, plr in pairs(game.Players:GetChildren()) do
            local char = plr.Character
            if plr.Team == teams['Red'] then
                char.HumanoidRootPart.CFrame = redSpawn.CFrame
            elseif plr.Team == teams['Blue'] then
                char.HumanoidRootPart.CFrame = blueSpawn.CFrame
            end
        end
    elseif inRound.Value == false then
        for _, plr in pairs(game.Players:GetPlayers()) do
            local char = plr.Character
            plr.Team = teams['Lobby']
            char.HumanoidRootPart.CFrame = lobbySpawn.CFrame
        end
    end
end)

function initWin(i)
    local redAlive = 0
    local blueAlive = 0
    for _, plr in pairs(game.Players:GetPlayers()) do
        if plr.Team == teams['Red'] then
            redAlive += 1
            redName = plr.Name
        elseif plr.Team == teams['Blue'] then
            blueAlive += 1
            blueName = plr.Name

        end
    end
    if (blueAlive == 0 and redAlive == 0) then
        status.Value = 'Tie!'
        game.ReplicatedStorage.Tie.Value = true
        wait(i)
    elseif blueAlive == 0 then
        status.Value = redName .. ' has won!'
        for _, plr in pairs(game.Players:GetPlayers()) do
            if plr.Team == teams['Red'] then
                plr.leaderstats.Wins.Value += 1
                plr.leaderstats.Suffering.Value += 200
                redWin.Value = true
            end
        end
        wait(i)
    elseif redAlive == 0 then
        status.Value = blueName .. ' has won!'
        for _, plr in pairs(game.Players:GetPlayers()) do
            if plr.Team == teams['Blue'] then
                plr.leaderstats.Wins.Value += 1
                plr.leaderstats.Suffering.Value += 200
                blueWin.Value = true

            end
        end
        wait(i)
    else
        return
    end
end

local function roundTimer()
    while wait() do
        --if #game.Players:GetPlayers() > 1 then
            for i = intermissionLength, 1, -1 do
                status.Value = 'Intermission: ' .. i .. ' seconds left'
                inRound.Value = false
                if i == intermissionLength/2 then
                    local bluePlayer = game.Player:GetPlayers()[math.random() * #teams.Lobby:GetPlayers()]
                    bluePlayer.Team = teams['Blue']
                    local redPlayer = game.Players:GetPlayers()[math.random() * #teams.Lobby:GetPlayers()]
                    redPlayer.Team = teams['Red']
                end
                wait(1)
            end
            for i = roundLength, 1, -1 do
                status.Value = 'Game:' .. i .. ' seconds left'
                inRound.Value = true
                initWin(10)
                if string.find(status.Value, '!') then
                    respawnTowers:Fire()
                    break
                end
            end
        --else
            --status.Value = 'Not enough players to start the game!'
        --end
    end
end

spawn(roundTimer)

I am making a round-based game. This script works in studio, but not in game. All of my other scripts work, but this is the main script and controls the main function of the game. I'm also not getting any errors from the output. FilteringEnabled is not the issue because I've tried setting it to false and it still doesn't work. Any help would be appreciated.

Answer this question