I’m making a ctf game and I want there to be a sound in the intermission. It works when you join in a new server, but not after. The actual game audio randomizer doesn’t work either, and it isnt showing any errors so i got no idea why it is happening.
Here is the code:
local GameManager = {}
local randChoice = math.random(0,5)
-- ROBLOX services
local Players = game.Players
-- Game services
local Configurations = require(game.ServerStorage.Configurations)
local TeamManager = require(script.TeamManager)
local PlayerManager = require(script.PlayerManager)
local MapManager = require(script.MapManager)
local TimeManager = require(script.TimeManager)
local DisplayManager = require(script.DisplayManager)
-- Local Variables
local IntermissionRunning = false
local EnoughPlayers = false
local GameRunning = false
local Events = game.ReplicatedStorage.Events
local CaptureFlag = Events.CaptureFlag
local ReturnFlag = Events.ReturnFlag
-- Local Functions
function OnCaptureFlag(player)
PlayerManager:AddPlayerScore(player, 1)
TeamManager:AddTeamScore(player.TeamColor, 1)
DisplayManager:DisplayNotification(player.TeamColor, 'Captured Flag!')
game.ReplicatedStorage.Events.FlagCaptureChat:FireAllClients(player)
end
local function OnReturnFlag(flagColor)
DisplayManager:DisplayNotification(flagColor, 'Flag Returned!')
end
-- Public Functions
function GameManager:Initialize()
MapManager:SaveMap()
end
function GameManager:RunIntermission()
IntermissionRunning = true
game.ReplicatedStorage.Events.IntermissionStartChat:FireAllClients()
game.SoundService.IntermissionTrack:Play()
TimeManager:StartTimer(Configurations.INTERMISSION_DURATION)
DisplayManager:StartIntermission()
EnoughPlayers = Players.NumPlayers >= Configurations.MIN_PLAYERS
DisplayManager:UpdateTimerInfo(true, not EnoughPlayers)
spawn(function()
repeat
if EnoughPlayers and Players.NumPlayers < Configurations.MIN_PLAYERS then
EnoughPlayers = false
elseif not EnoughPlayers and Players.NumPlayers >= Configurations.MIN_PLAYERS then
EnoughPlayers = true
end
DisplayManager:UpdateTimerInfo(true, not EnoughPlayers)
wait(.5)
until IntermissionRunning == false
end)
wait(Configurations.INTERMISSION_DURATION)
IntermissionRunning = false
end
function GameManager:StopIntermission()
--IntermissionRunning = false
game.SoundService.IntermissionTrack:Stop()
DisplayManager:UpdateTimerInfo(false, false)
DisplayManager:StopIntermission()
end
function GameManager:GameReady()
return Players.NumPlayers >= Configurations.MIN_PLAYERS
end
function GameManager:StartRound()
TeamManager:ClearTeamScores()
PlayerManager:ClearPlayerScores()
PlayerManager:AllowPlayerSpawn(true)
PlayerManager:LoadPlayers()
GameRunning = true
PlayerManager:SetGameRunning(true)
TimeManager:StartTimer(Configurations.ROUND_DURATION)
game.ReplicatedStorage.Events.GameStartChat:FireAllClients()
if randChoice == 1 then
game.SoundService.GameAudio1:Play()
elseif randChoice == 2 then
game.SoundService.GameAudio2:Play()
elseif randChoice == 3 then
game.SoundService.GameAudio3:Play()
elseif randChoice == 4 then
game.SoundService.GameAudio4:Play()
elseif randChoice == 5 then
game.SoundService.GameAudio5:Play()
end
--[[
game.Players.PlayerAdded:Connect(function(player)
playerNum = 0
for i,v in pairs(game.Players:GetChildren()) do
playerNum += 1
end
game.StarterGui:SetCore("ChatMakeSystemMessage", {
Text = "To win, your team needs to capture ".. tostring(math.round(playerNum / 2)) .." flags.",
Font = Enum.Font.SourceSansBold,
Color = BrickColor.new("New Yeller"),
FontSize = Enum.FontSize.Size96
})
end)
game.Players.PlayerRemoving:Connect(function(player)
playerNum = 0
for i,v in pairs(game.Players:GetChildren()) do
playerNum += 1
end
game.StarterGui:SetCore("ChatMakeSystemMessage", {
Text = "To win, your team needs to capture ".. tostring(playerNum / 2) .." flags.",
Font = Enum.Font.SourceSansBold,
Color = BrickColor.new("New Yeller"),
FontSize = Enum.FontSize.Size96
})
end)
]]
end
function GameManager:Update()
--TODO: Add custom custom game code here
end
function GameManager:RoundOver()
local winningTeam = TeamManager:HasTeamWon()
if winningTeam then
DisplayManager:DisplayVictory(winningTeam)
for i,v in pairs(game.Players:GetPlayers()) do
if v.Team == winningTeam then
v:FindFirstChild("leaderstats").Coins.Value += math.random(0,100)
end
end
game.ReplicatedStorage.Events.OnWinningTeamChat:FireAllClients(winningTeam)
game.ReplicatedStorage.Events.TeamWonChat:FireAllClients(winningTeam)
return true
end
if TimeManager:TimerDone() then
if TeamManager:AreTeamsTied() then
DisplayManager:DisplayVictory('Tie')
game.ReplicatedStorage.Events.TieChat:FireAllClients()
else
winningTeam = TeamManager:GetWinningTeam()
DisplayManager:DisplayVictory(winningTeam)
game.ReplicatedStorage.Events.OnWinningTeamChat:FireAllClients(winningTeam)
game.ReplicatedStorage.Events.TeamWonChat:FireAllClients(winningTeam)
end
return true
end
if randChoice == 1 then
game.SoundService.GameAudio1:Stop()
game.SoundService.GameAudio1.TimePosition = 0
elseif randChoice == 2 then
game.SoundService.GameAudio2:Stop()
game.SoundService.GameAudio2.TimePosition = 0
elseif randChoice == 3 then
game.SoundService.GameAudio3:Stop()
game.SoundService.GameAudio3.TimePosition = 0
elseif randChoice == 4 then
game.SoundService.GameAudio4:Stop()
game.SoundService.GameAudio4.TimePosition = 0
elseif randChoice == 5 then
game.SoundService.GameAudio5:Stop()
game.SoundService.GameAudio5.TimePosition = 0
end
return false
end
function GameManager:RoundCleanup()
PlayerManager:SetGameRunning(false)
wait(Configurations.END_GAME_WAIT)
PlayerManager:AllowPlayerSpawn(false)
PlayerManager:DestroyPlayers()
DisplayManager:DisplayVictory(nil)
TeamManager:ClearTeamScores()
PlayerManager:ClearPlayerScores()
TeamManager:ShuffleTeams()
MapManager:ClearMap()
MapManager:LoadMap()
end
-- Bind Events
CaptureFlag.Event:connect(OnCaptureFlag)
ReturnFlag.Event:connect(OnReturnFlag)
return GameManager