The problem is that the script runs for one player at a time. After finishing with one player, it moves on to the next player. This causes the actions to happen one by one for each player, instead of all at once or individually for each player right away. Any ideas on how to fix this?
local Teams = game:GetService("Teams")
local ServerS = game:GetService("ServerStorage")
local RepStorage = game:GetService("ReplicatedStorage")
local function areAllPlayersDead(team)
local allDead = true
for _, player in ipairs(team:GetPlayers()) do
if player.Character and player.Character:FindFirstChild("Humanoid") and player.Character.Humanoid.Health > 0 then
allDead = false
break
end
end
return allDead
end
local function onTeamEliminated(teamName)
if teamName == "CT" then
if RepStorage.Values.GameStarted.Value == true then
RepStorage.Events2.CSWin:Fire("CT")
RepStorage.Values.GameStarted.Value = false
end
else
if RepStorage.Values.GameStarted.Value == true then
RepStorage.Events2.CSWin:Fire("T")
RepStorage.Values.GameStarted.Value = false
end
end
end
local function monitorTeamElimination()
local teamT = Teams:FindFirstChild("T")
local teamCT = Teams:FindFirstChild("CT")
if teamT and areAllPlayersDead(teamT) then
onTeamEliminated(teamT.Name)
end
if teamCT and areAllPlayersDead(teamCT) then
onTeamEliminated(teamCT.Name)
end
end
local function setupPlayerDeathMonitoring(player)
player.CharacterAdded:Connect(function(character)
local humanoid = character:FindFirstChildOfClass("Humanoid")
if humanoid then
humanoid.Died:Connect(function()
if RepStorage.Values.GameStarted.Value then
monitorTeamElimination()
end
end)
end
end)
end
local function typewrite(instance, text)
for i = 1, #text,1 do
instance.Text = string.sub(text,1,i)
local TickSound = RepStorage.Storage.UISounds.tick_sound:Clone()
TickSound.Parent = workspace
TickSound:Play()
TickSound.Ended:Connect(function()
TickSound:Destroy()
end)
task.wait(0.1)
end
end
RepStorage.Events2.CSWin.Event:Connect(function(TeamName)
if RepStorage.Values.GameStarted.Value == true then
for _, Playersz in ipairs(game:GetService("Players"):GetPlayers()) do
if Playersz:IsA("Player") then
if TeamName == "T" then
RepStorage.Storage.UISounds.TWins:Play()
typewrite(Playersz.PlayerGui.Hints.Frame.Timer, "TERRORISTS WIN.")
task.wait(5)
typewrite(Playersz.PlayerGui.Hints.Frame.Timer, "")
else
RepStorage.Storage.UISounds.CTWins:Play()
typewrite(Playersz.PlayerGui.Hints.Frame.Timer, "COUNTER-TERRORISTS WIN.")
task.wait(5)
typewrite(Playersz.PlayerGui.Hints.Frame.Timer, "")
end
end
end
end
end)
RepStorage.Values.GameStarted:GetPropertyChangedSignal("Value"):Connect(function()
if RepStorage.Values.GameStarted.Value == false then
for _, Playersz in pairs(game:GetService("Players"):GetPlayers()) do
task.wait(2)
monitorTeamElimination()
local PCharacterz = Playersz.Character
local Humanoid: Humanoid = PCharacterz:WaitForChild("Humanoid")
local Boom = RepStorage.Storage.UISounds.boom:Clone()
Boom.Parent = workspace
Boom:Play()
Boom.Ended:Connect(function()
Boom:Destroy()
end)
task.spawn(function()
local Teams = game:GetService("Teams")
local teamT = Teams:FindFirstChild("T")
if not teamT then
warn("Team 'T' not found")
return
end
local teamPlayers = {}
for _, player in ipairs(game:GetService("Players"):GetPlayers()) do
if player.Team == teamT then
table.insert(teamPlayers, player)
end
end
if #teamPlayers > 0 then
local randomIndex = math.random(#teamPlayers)
local randomPlayer = teamPlayers[randomIndex]
if randomPlayer and randomPlayer.Character then
local toolName = "C4"
local tool = ServerS.Tools:WaitForChild("C4")
if tool then
local toolClone = tool:Clone()
toolClone.Parent = randomPlayer.Backpack
print(randomPlayer.Name .. " from Team T has received " .. toolName)
else
warn("Tool named '" .. toolName .. "' not found in RepStorage.Storage.Tools")
end
end
else
print("No players in Team T to receive the tool.")
end
end)
local SetTimer = 60
Playersz:LoadCharacter()
typewrite(Playersz.PlayerGui.Hints.Frame.Hint, "GEAR UP")
task.wait(1)
while SetTimer > 0 do
local TickSound = RepStorage.Storage.UISounds.tick_sound:Clone()
TickSound.Parent = workspace
TickSound:Play()
TickSound.Ended:Connect(function()
TickSound:Destroy()
end)
local TimerText = Playersz.PlayerGui.Hints.Frame.Timer
TimerText.Text = SetTimer
task.wait(1)
SetTimer -= 1
if SetTimer <= 0 then
local Boom = RepStorage.Storage.UISounds.boom:Clone()
Boom.Parent = workspace
Boom:Play()
Boom.Ended:Connect(function()
Boom:Destroy()
end)
SetTimer = 10
RepStorage.Values.GameStarted.Value = true
typewrite(Playersz.PlayerGui.Hints.Frame.Hint, "GET READY")
while SetTimer > 0 do
local TickSound = RepStorage.Storage.UISounds.typewriter:Clone()
TickSound.Parent = workspace
TickSound.PlaybackSpeed = math.random(0.9,1.1)
TickSound:Play()
TickSound.Ended:Connect(function()
TickSound:Destroy()
end)
local TimerText = Playersz.PlayerGui.Hints.Frame.Timer
TimerText.Text = SetTimer
task.wait(1)
SetTimer -= 1
if SetTimer <= 0 then
local Boom = RepStorage.Storage.UISounds.boom:Clone()
Boom.Parent = workspace
Boom:Play()
Boom.Ended:Connect(function()
Boom:Destroy()
end)
Playersz.PlayerGui.Hints.Frame.Hint.Text = ""
Playersz.PlayerGui.Hints.Frame.Timer.Text = ""
for index2, Borders in ipairs(workspace:WaitForChild("Filter"):WaitForChild("Parts"):WaitForChild("F_Parts"):WaitForChild("Border"):GetDescendants()) do
if Borders:IsA("Part") then
local OGTransparency = Borders.Transparency
Borders.CanCollide = false
Borders.CanQuery = false
Borders.Transparency = 1
end
end
end
end
end
end
end
end
end)
game:GetService("Players").PlayerAdded:Connect(function(player)
setupPlayerDeathMonitoring(player)
player.CharacterAdded:Connect(function(character)
local GameValues = game:GetService("ReplicatedStorage").Values
if not GameValues.GameStarted.Value then
if player.Team.Name == "CT" then
if not GameValues.GameStarted.Value then
task.wait(2)
local Bayoney = ServerS.Useless.Bayonet:Clone()
Bayoney.Parent = player.Backpack
local FNP = ServerS.Tools["FNP-45"]:Clone()
FNP.Parent = player.Backpack
player.PlayerGui:WaitForChild("GunSelector").Enabled = true
player.PlayerGui:WaitForChild("GunSelector").CT.GunSelectorFrame.Visible = true
end
elseif player.Team.Name == "T" then
if not GameValues.GameStarted.Value then
task.wait(2)
local Bayoney = ServerS.Useless.Bayonet:Clone()
Bayoney.Parent = player.Backpack
local G17 = ServerS.Tools["G-17"]:Clone()
G17.Parent = player.Backpack
player.PlayerGui:WaitForChild("GunSelector").Enabled = true
player.PlayerGui:WaitForChild("GunSelector").T.GunSelectorFrame.Visible = true
end
end
end
end)
end)
RepStorage.Values.GameStarted:GetPropertyChangedSignal("Value"):Connect(function()
if RepStorage.Values.GameStarted.Value == false then
for _, Playersz in pairs(game:GetService("Players"):GetPlayers()) do
task.wait(2)
monitorTeamElimination()
local PCharacterz = Playersz.Character
local Humanoid: Humanoid = PCharacterz:WaitForChild("Humanoid")
local Boom = RepStorage.Storage.UISounds.boom:Clone()
Boom.Parent = workspace
Boom:Play()
Boom.Ended:Connect(function()
Boom:Destroy()
end)
task.spawn(function()
local Teams = game:GetService("Teams")
local teamT = Teams:FindFirstChild("T")
if not teamT then
warn("Team 'T' not found")
return
end
local teamPlayers = {}
for _, player in ipairs(game:GetService("Players"):GetPlayers()) do
if player.Team == teamT then
table.insert(teamPlayers, player)
end
end
if #teamPlayers > 0 then
local randomIndex = math.random(#teamPlayers)
local randomPlayer = teamPlayers[randomIndex]
if randomPlayer and randomPlayer.Character then
local toolName = "C4"
local tool = ServerS.Tools:WaitForChild("C4")
if tool then
local toolClone = tool:Clone()
toolClone.Parent = randomPlayer.Backpack
print(randomPlayer.Name .. " from Team T has received " .. toolName)
else
warn("Tool named '" .. toolName .. "' not found in RepStorage.Storage.Tools")
end
end
else
print("No players in Team T to receive the tool.")
end
end)
local SetTimer = 60
Playersz:LoadCharacter()
typewrite(Playersz.PlayerGui.Hints.Frame.Hint, "GEAR UP")
task.wait(1)
while SetTimer > 0 do
local TickSound = RepStorage.Storage.UISounds.tick_sound:Clone()
TickSound.Parent = workspace
TickSound:Play()
TickSound.Ended:Connect(function()
TickSound:Destroy()
end)
local TimerText = Playersz.PlayerGui.Hints.Frame.Timer
TimerText.Text = SetTimer
task.wait(1)
SetTimer -= 1
if SetTimer <= 0 then
local Boom = RepStorage.Storage.UISounds.boom:Clone()
Boom.Parent = workspace
Boom:Play()
Boom.Ended:Connect(function()
Boom:Destroy()
end)
SetTimer = 10
RepStorage.Values.GameStarted.Value = true
typewrite(Playersz.PlayerGui.Hints.Frame.Hint, "GET READY")
while SetTimer > 0 do
local TickSound = RepStorage.Storage.UISounds.typewriter:Clone()
TickSound.Parent = workspace
TickSound.PlaybackSpeed = math.random(0.9,1.1)
TickSound:Play()
TickSound.Ended:Connect(function()
TickSound:Destroy()
end)
local TimerText = Playersz.PlayerGui.Hints.Frame.Timer
TimerText.Text = SetTimer
task.wait(1)
SetTimer -= 1
if SetTimer <= 0 then
local Boom = RepStorage.Storage.UISounds.boom:Clone()
Boom.Parent = workspace
Boom:Play()
Boom.Ended:Connect(function()
Boom:Destroy()
end)
Playersz.PlayerGui.Hints.Frame.Hint.Text = ""
Playersz.PlayerGui.Hints.Frame.Timer.Text = ""
for index2, Borders in ipairs(workspace:WaitForChild("Filter"):WaitForChild("Parts"):WaitForChild("F_Parts"):WaitForChild("Border"):GetDescendants()) do
if Borders:IsA("Part") then
local OGTransparency = Borders.Transparency
Borders.CanCollide = false
Borders.CanQuery = false
Borders.Transparency = 1
end
end
end
end
end
end
end
end
end)
I havent read the entire script, but if i had to guess, i think it’d have something to do with threading, when you use a wait() inside of a function, or script, the whole function or script will actually wait for it to finish before continuing, this is where multi-threading is useful, multi-threading allows you to use wait() without pausing the entire script, the function for that is task.spawn()
For example, lets say you have a for loop, and you want to add a wait.
– without task.spawn(), this pauses the entire script until the for loop finishes.
local function spawnBlock()
Instance.new("Part", workspace)
end
for i = 1, 5, 1 do
spawnBlock()
wait(1)
end
-- waits til the loop finishes, which would take 5 seconds
print("Finished")
– will print finished immediatly, and the for loop will still wait
local function spawnBlock()
Instance.new("Part", workspace)
end
for i = 1, 5, 1 do
task.spawn(function()
spawnBlock()
wait(1)
end)
end
-- prints finished immediatly and the parts will still spawn every second cause the wait is still in the loop.
print("Finished")
I tried it but its still the same, this is extract from the script is supposed to play at the same and run the code for all the players as soon the value gets changed to false
It will complete the loop before moving on to the next player. So, if you have a 2 second wait, that 2 second wait will happen each time the loop runs (for each player).
You could wrap the details in a coroutine so the loop can move on to the next player without having to wait. See below for example:
RepStorage.Values.GameStarted:GetPropertyChangedSignal("Value"):Connect(function()
if RepStorage.Values.GameStarted.Value == false then
for _, Playersz in pairs(game:GetService("Players"):GetPlayers()) do
--===========
-- COROUTINE
--===========
local Coroutine = coroutine.wrap(function()
task.wait(2)
monitorTeamElimination()
local PCharacterz = Playersz.Character
local Humanoid: Humanoid = PCharacterz:WaitForChild("Humanoid")
local Boom = RepStorage.Storage.UISounds.boom:Clone()
Boom.Parent = workspace
Boom:Play()
Boom.Ended:Connect(function()
Boom:Destroy()
end)
task.spawn(function()
local Teams = game:GetService("Teams")
local teamT = Teams:FindFirstChild("T")
if not teamT then
warn("Team 'T' not found")
return
end
local teamPlayers = {}
for _, player in ipairs(game:GetService("Players"):GetPlayers()) do
if player.Team == teamT then
table.insert(teamPlayers, player)
end
end
if #teamPlayers > 0 then
local randomIndex = math.random(#teamPlayers)
local randomPlayer = teamPlayers[randomIndex]
if randomPlayer and randomPlayer.Character then
local toolName = "C4"
local tool = ServerS.Tools:WaitForChild("C4")
if tool then
local toolClone = tool:Clone()
toolClone.Parent = randomPlayer.Backpack
print(randomPlayer.Name .. " from Team T has received " .. toolName)
else
warn("Tool named '" .. toolName .. "' not found in RepStorage.Storage.Tools")
end
end
else
print("No players in Team T to receive the tool.")
end
end)
local SetTimer = 60
Playersz:LoadCharacter()
typewrite(Playersz.PlayerGui.Hints.Frame.Hint, "GEAR UP")
task.wait(1)
while SetTimer > 0 do
local TickSound = RepStorage.Storage.UISounds.tick_sound:Clone()
TickSound.Parent = workspace
TickSound:Play()
TickSound.Ended:Connect(function()
TickSound:Destroy()
end)
local TimerText = Playersz.PlayerGui.Hints.Frame.Timer
TimerText.Text = SetTimer
task.wait(1)
SetTimer -= 1
if SetTimer <= 0 then
local Boom = RepStorage.Storage.UISounds.boom:Clone()
Boom.Parent = workspace
Boom:Play()
Boom.Ended:Connect(function()
Boom:Destroy()
end)
SetTimer = 10
RepStorage.Values.GameStarted.Value = true
typewrite(Playersz.PlayerGui.Hints.Frame.Hint, "GET READY")
while SetTimer > 0 do
local TickSound = RepStorage.Storage.UISounds.typewriter:Clone()
TickSound.Parent = workspace
TickSound.PlaybackSpeed = math.random(0.9,1.1)
TickSound:Play()
TickSound.Ended:Connect(function()
TickSound:Destroy()
end)
local TimerText = Playersz.PlayerGui.Hints.Frame.Timer
TimerText.Text = SetTimer
task.wait(1)
SetTimer -= 1
if SetTimer <= 0 then
local Boom = RepStorage.Storage.UISounds.boom:Clone()
Boom.Parent = workspace
Boom:Play()
Boom.Ended:Connect(function()
Boom:Destroy()
end)
Playersz.PlayerGui.Hints.Frame.Hint.Text = ""
Playersz.PlayerGui.Hints.Frame.Timer.Text = ""
for index2, Borders in ipairs(workspace:WaitForChild("Filter"):WaitForChild("Parts"):WaitForChild("F_Parts"):WaitForChild("Border"):GetDescendants()) do
if Borders:IsA("Part") then
local OGTransparency = Borders.Transparency
Borders.CanCollide = false
Borders.CanQuery = false
Borders.Transparency = 1
end
end
end
end
end
end
end)
Coroutine()
end
end
end)