What do you want to achieve?
I want the current round to end and restart if every player dies or leaves the current game
What is the issue?
Currently if all players die they transport back to the lobby but the round timer still finishes up before restarting. This is kind of boring to have to wait extra long if no one is still in the game playing.
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I tried to research how to write this into my existing timer script but I just don’t even know what to write to do it
My Current script is below:
local lobby_Duration = 20
local round_Duration = 200
local map_Fol = game.ReplicatedStorage:WaitForChild("Map")
local timerval = game.ReplicatedStorage:WaitForChild("TimerValue")
local function RTimer ()
while wait() do
for l = lobby_Duration, 0, -1 do
timerval.Value = 'Intermission: '..l
wait(1)
if l == 0 then
--local map = map_Fol:WaitForChild('Main') -- change to your map name
local mapCopy = map_Fol:Clone()
mapCopy.Name = "mapCopy"
mapCopy.Parent = game.Workspace
timerval.Value = 'Teleporting players to '..mapCopy.Name
wait(1)
mapCopy.Parent = workspace
local plrs = game.Players:GetChildren()
for i = 1, #plrs do
plrs[i].Character:MoveTo(Vector3.new(-15.254, 6.232, 130.566))
end
local Players = game:GetService("Players")
local Workspace = game:GetService("Workspace")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Event = ReplicatedStorage:WaitForChild("DisplayRole")
local Sword = ReplicatedStorage:WaitForChild("Sword")
local Killer = nil
local Survivors = {}
function pickPlayers()
if Killer then
if Players:FindFirstChild(Killer) then
if Players[Killer].Bakcpack:FindFirstChild(Sword.Name) then
Players[Killer].Bakcpack[Sword.Name]:Destroy()
else
if Workspace:FindFirstChild(Killer) then
if Workspace[Killer]:FindFirstChild(Sword.Name) then
Workspace[Killer][Sword.Name]:Destroy()
end
end
end
end
end
Killer = nil
Survivors = {}
local PlayersGroup = Players:GetChildren()
local KillerID = math.random(1, #PlayersGroup)
for i, v in pairs(PlayersGroup) do
if i == KillerID then
Killer = v.Name
break
end
end
for i, v in pairs(PlayersGroup) do
if i == KillerID then
else
table.insert(Survivors, v.Name)
end
end
Event:FireClient(Players[Killer], "You are Killer")
Sword:Clone().Parent = Players[Killer].Backpack
print("Killer: ".. Killer)
for i, v in pairs(Survivors) do
Event:FireClient(Players[v], "You are Survivor")
print("Survivor: ".. v)
end
end
pickPlayers()
wait(1)
for g = round_Duration,0, -1 do
timerval.Value = 'Round '..g..' seconds left'
wait(1)
if g == 0 then
timerval.Value = 'Round over'
wait(2)
local function cleanup()
game.Workspace.mapCopy:Destroy()
end
cleanup()
timerval.Value = 'Teleporting players to lobby'
wait(1)
for i = 1, #plrs do
plrs[i].Character:MoveTo(Vector3.new(1097.553, -36.89, -26.611))
end
--map.Parent = map_Fol
wait(1)
-- repeat again
end
end
end
end
end
end
spawn(RTimer)
Insert all of the player names into that table. Then, when they die or leave, remove them from the table. Just check if the player is empty, and it should work.
First, hold all of the players in a table as @Infinite_Visions mentioned.
Since you’re creating a separate thread for your RTimer function, you should be able to use the players.PlayerRemoving event, you can then create a global to tell your RTimer event to stop.
local players = game:GetService('Players')
local ROUND_ENDS = false
-- when your round starts, put all of the players in a table.
local Players = players:GetPlayers()
players.PlayerRemoving:Connect(function(player)
if #Players <= 0 then
ROUND_ENDS = true
-- you could then add anything you'd need for the round to end
end
end)
-- skipping ahead to your loop
for g = round_Duration,0, -1 do
timerval.Value = 'Round '..g..' seconds left'
if ROUND_ENDS then
break -- break tells the loop to stop
end
wait(1)
if g == 0 or TIMER_ENDS then
timerval.Value = 'Round over'
wait(2)
local function cleanup()
game.Workspace.mapCopy:Destroy()
end
cleanup()
timerval.Value = 'Teleporting players to lobby'
wait(1)
for i = 1, #plrs do
plrs[i].Character:MoveTo(Vector3.new(1097.553, -36.89, -26.611))
end
--map.Parent = map_Fol
wait(1)
-- repeat again
end
Ok I’ve tried incorporating your suggestions into my existing script but I’m obviously not placing in the appropriate places because it ends up breaking the timer.
If all the players leave the server will shutdown. For checking if all the players have died you can use a table for all the players in the current round and remove them when they die.
I appreciate yall’s responses and help so far…
I understand what you are saying, however I’m new at scripting so I still have a hard time knowing exactly how to write and in which order to place it in my already working script. I can read and interpret my current script and know what it is doing. But now trying to add this new thing into the script, I am not good at knowing where to place it without breaking the script.
I know which part of my script deals with the round timer yet I end up having to move things around 50 different times and ways before I can get it working. That is what I already did for the script I’m using when I incorporated the killer and survivor table into the timer script. I tried like 50 different ways, none of which worked, they all continually broke the script. then by mere luck of the draw on try number 51 it worked… shewy and whew and yay all at the same time on my part.
So that is what I see myself doing again to incorporate this into the script so I can get my round to refresh and start over throwing all the players back into the lobby and restart mode instead of just throwing all players back to the lobby but still having to wait until the existing games timer runs out.
Also if the game is already in session no one else can join the current round, bummer, they have to wait until the current round is finished. But that is another issue entirely, one that I will have to figure out and work on next I imagine.
I played around with the Player table and Player Removing, however it just seems to me since I already had the Survivors table setup, wouldn’t that be able to be used ? I tried messing with that as well but no matter what I tried it just wouldn’t work. It either broke the time or just did nothing.
Still trying… but I’m obviously missing something vital to making it work.
I have ** notated with asterics ** where I started to make the table in my round for implementing this but it just is appearing like I don’t know how to write it in or incorporate it in. I was trying to get the table to print the UserId. Right now all it will say is Players.
Also I have a question. Since I already am picking a Killer and Survivors, why can’t I use the Survivor table for the reset of the timer ? When all Survivors which are players leave the game or die the table automatically resets. In this thread Ive been pointed toward making a new table for players. It just seems redundant to me ?
My attempts to write this in have been unsuccessful so far. I’ve tried following the instructions given, especially the ones provided by 7z99, I did all of your suggestions step by step trying to get it written into my script but I just can’t get it written in and working.
local lobby_Duration = 20
local round_Duration = 200
local map_Fol = game.ReplicatedStorage:WaitForChild("Map")
local timerval = game.ReplicatedStorage:WaitForChild("TimerValue")
local function RTimer ()
while wait() do
for l = lobby_Duration, 0, -1 do
timerval.Value = 'Intermission: '..l
wait(1)
if l == 0 then
--local map = map_Fol:WaitForChild('Main') -- change to your map name
local mapCopy = map_Fol:Clone()
mapCopy.Name = "mapCopy"
mapCopy.Parent = game.Workspace
timerval.Value = 'Teleporting players to '..mapCopy.Name
wait(1)
mapCopy.Parent = workspace
wait(2)
local plrs = game.Players:GetChildren()
for i = 1, #plrs do
plrs[i].Character:MoveTo(Vector3.new(-15.254, 6.232, 130.566))
end
local Players = game:GetService("Players")
local Workspace = game:GetService("Workspace")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Event = ReplicatedStorage:WaitForChild("DisplayRole")
local Sword = ReplicatedStorage:WaitForChild("Sword")
local Killer = nil
local Survivors = {}
function pickPlayers()
if Killer then
if Players:FindFirstChild(Killer) then
if Players[Killer].Bakcpack:FindFirstChild(Sword.Name) then
Players[Killer].Bakcpack[Sword.Name]:Destroy()
else
if Workspace:FindFirstChild(Killer) then
if Workspace[Killer]:FindFirstChild(Sword.Name) then
Workspace[Killer][Sword.Name]:Destroy()
end
end
end
end
end
Killer = nil
Survivors = {}
local PlayersGroup = Players:GetChildren()
local KillerID = math.random(1, #PlayersGroup)
for i, v in pairs(PlayersGroup) do
if i == KillerID then
Killer = v.Name
break
end
end
for i, v in pairs(PlayersGroup) do
if i == KillerID then
else
table.insert(Survivors, v.Name)
end
end
Event:FireClient(Players[Killer], "You are Killer")
Sword:Clone().Parent = Players[Killer].Backpack
print("Killer: ".. Killer)
for i, v in pairs(Survivors) do
Event:FireClient(Players[v], "You are Survivor")
print("Survivor: ".. v)
end
end
pickPlayers()
wait(1)
**local players = game:GetService("Players")**
for g = round_Duration,0, -1 do
timerval.Value = 'Round '..g..' seconds left'
wait(1)
**local Players = Players:GetPlayers()**
--players.PlayerRemoving:Connect(function(Players)
**print("Players")**
-- you could then add anything you'd need for the round to end
--end)
--local Players = {}
if g == 0 then
timerval.Value = 'Round over'
wait(2)
local function cleanup()
game.Workspace.mapCopy:Destroy()
end
cleanup()
timerval.Value = 'Teleporting players to lobby'
wait(1)
for i = 1, #plrs do
plrs[i].Character:MoveTo(Vector3.new(1097.553, -36.89, -26.611))
end
--map.Parent = map_Fol
wait(1)
-- repeat again
end
end
end
end
end
end
spawn(RTimer)
Well I changed something else in my script so that I’ve accomplished a table being created once the round starts. It is showing me the player in the table now. So my next step to figure out is how to remove it from the table and get the timer to restart.
local lobby_Duration = 20
local round_Duration = 200
local map_Fol = game.ReplicatedStorage:WaitForChild("Map")
local timerval = game.ReplicatedStorage:WaitForChild("TimerValue")
local function RTimer ()
while wait() do
for l = lobby_Duration, 0, -1 do
timerval.Value = 'Intermission: '..l
wait(1)
if l == 0 then
--local map = map_Fol:WaitForChild('Main') -- change to your map name
local mapCopy = map_Fol:Clone()
mapCopy.Name = "mapCopy"
mapCopy.Parent = game.Workspace
timerval.Value = 'Teleporting players to '..mapCopy.Name
wait(1)
mapCopy.Parent = workspace
wait(2)
local plrs = game.Players:GetChildren()
for i = 1, #plrs do
plrs[i].Character:MoveTo(Vector3.new(-15.254, 6.232, 130.566))
end
local Players = game:GetService("Players")
local Workspace = game:GetService("Workspace")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Event = ReplicatedStorage:WaitForChild("DisplayRole")
local Sword = ReplicatedStorage:WaitForChild("Sword")
local Killer = nil
local Survivors = {}
function pickPlayers()
if Killer then
if Players:FindFirstChild(Killer) then
if Players[Killer].Bakcpack:FindFirstChild(Sword.Name) then
Players[Killer].Bakcpack[Sword.Name]:Destroy()
else
if Workspace:FindFirstChild(Killer) then
if Workspace[Killer]:FindFirstChild(Sword.Name) then
Workspace[Killer][Sword.Name]:Destroy()
end
end
end
end
end
Killer = nil
Survivors = {}
local PlayersGroup = Players:GetChildren()
local KillerID = math.random(1, #PlayersGroup)
for i, v in pairs(PlayersGroup) do
if i == KillerID then
Killer = v.Name
break
end
end
for i, v in pairs(PlayersGroup) do
if i == KillerID then
else
table.insert(Survivors, v.Name)
end
end
Event:FireClient(Players[Killer], "You are Killer")
Sword:Clone().Parent = Players[Killer].Backpack
print("Killer: ".. Killer)
for i, v in pairs(Survivors) do
Event:FireClient(Players[v], "You are Survivor")
print("Survivor: ".. v)
end
end
pickPlayers()
wait(1)
local players = game:GetService("Players")
for g = round_Duration,0, -1 do
timerval.Value = 'Round '..g..' seconds left'
wait(1)
local Players = Players:GetPlayers()
--players.PlayerRemoving:Connect(function(Players)
print(Players)
-- you could then add anything you'd need for the round to end
--end)
--local Players = {}
if g == 0 then
timerval.Value = 'Round over'
wait(2)
local function cleanup()
game.Workspace.mapCopy:Destroy()
end
cleanup()
timerval.Value = 'Teleporting players to lobby'
wait(1)
for i = 1, #plrs do
plrs[i].Character:MoveTo(Vector3.new(1097.553, -36.89, -26.611))
end
--map.Parent = map_Fol
wait(1)
-- repeat again
end
end
end
end
end
end
spawn(RTimer)
I have a game with the loop cycle, it had not occurred to me to implement the system that if everyone dies it ends, but you should do so:
local PlayersCount = Services.Players:GetPlayers()
local playersInRound = {} – track alive players
local connections = {} – store connections to disconnect later
for _, Player in pairs(Players:GetChildren()) do
if Player.Character and Player.Character:FindFirstChild(“Humanoid”) then
table.insert(playersInRound, Player)----
connections[Player.Name] = Player.Character.Humanoid.Died:Connect(function()
table.remove(playersInRound, table.find(playersInRound, Player))
end)
end
end
connections[“Removing”] = game.Players.PlayerRemoving:Connect(function(player) – remove player from list if they leave
local ind = table.find(playersInRound, player)
if ind then
table.remove(playersInRound, ind)
end
end)
if #playersInRound <= 1 then
– function () or directly set the time to 0
end