I’m making an obby-ish game where you try to be the last person alive. When you die or just spawn in the game, you go to the lobby. After the 90 game time is over, you are teleported into the actual gameplay part of the game. The time remaining in lobby or in gameplay time is displayed on screen. Whether there is only one person left or nobody left, the game time keeps going. I want to adjust it so that the game ends
If nobody is left
If only one person is left
If the gameplay time runs out
and so that I can get the player who survived longest
My server script:
local roundlength = 90
local intermissionlength = 10
local InRound = game.ReplicatedStorage.InRound
local Status = game.ReplicatedStorage.Status
local LobbySpawn = game.Workspace.LobbySpawn
local GameAreaSpawn = game.Workspace.GameAreaSpawn
InRound.Changed:Connect(function()
if InRound.Value == true then
for _, player in pairs(game.Players:GetChildren()) do
local char = player.Character
char.HumanoidRootPart.CFrame = GameAreaSpawn.CFrame
end
else
for _, player in pairs(game.Players:GetChildren()) do
local char = player.Character
char.HumanoidRootPart.CFrame = LobbySpawn.CFrame
char:FindFirstChild("Humanoid").Health = 0
end
end
end)
local function RoundTimer()
while wait() do
for i = intermissionlength,0,-1 do
InRound.Value = false
wait(1)
Status.Value = "Intermission: ".. i .." seconds"
end
for i = roundlength,0,-1 do
InRound.Value = true
wait(1)
Status.Value = "Game: ".. i .. " seconds"
end
end
end
spawn(RoundTimer)
I have one boolvalue in replicated storage names “InRound” and a stringvalue named “Status”. In StarterGui I have a ScreenGui named “Timer” with a local script and text label under it.
My local script:
local Status = game.ReplicatedStorage.Status
local TimerDisplay = script.Parent.TimerDisplay
Status.Changed:Connect(function()
TimerDisplay.Text = Status.Value
end)
I’m not very sure if I have to change up this whole thing (which I copied from some video) or just make adjustments as I’m pretty new to scripting. Thank you!
Once they reach the end of the obby part, they get a sword. If they get the sword and don’t die before the game ends, they will keep the sword when teleported into the lobby area, which I don’t want. So I kill them to erase the sword from their inventory.
When you die or just spawn in the game, you go to the lobby.
Yea that is something I want.
After the 90 game time is over, you are teleported into the actual gameplay part of the game.
The 90 secs of game time or 10 secs of intermission are still going when you spawn in the game, so you can’t instantly play. After the 90 seconds of game time and 10 seconds of intermission time are done, you are able to go into the gameplay part of the game, which is over that 90 secs of game time.
Whether there is only one person left or nobody left, the game time keeps going.
So far, after revising your script, I have made a few changes.
What changes I made
#1: First off, you were killing the player at the end. Instead, I changed it to player.Backpack:ClearAllChildren() to clear the backpack.
#2: You were using game.Players:GetChildren() to get the players. A better way to do this would be game.Players:GetPlayers() , as that only gets the player objects, not any other instances.
#3: For when the value is changed, you used .Changed. However, the changed event fires when ANY property or attribute on the object is changed. I made this :GetPropertyChangedSignal(“Value”) to only fire then the value property of the IntValue (or NumberValue, I don’t know which type it is) is changed.
#4: basically just some basic stuff that i dont want to type because my fingers hurt
New Script
local roundlength = 90
local intermissionlength = 10
local InRound = game.ReplicatedStorage.InRound
local Status = game.ReplicatedStorage.Status
local LobbySpawn = game.Workspace.LobbySpawn
local GameAreaSpawn = game.Workspace.GameAreaSpawn
InRound:GetPropertyChangedSignal("Value"):Connect(function()
if InRound.Value == true then
for _, player in pairs(game.Players:GetPlayers()) do
local char = player.Character
char.HumanoidRootPart.CFrame = GameAreaSpawn.CFrame
end
else
for _, player in pairs(game.Players:GetPlayers()) do
local char = player.Character
char.HumanoidRootPart.CFrame = LobbySpawn.CFrame
player.Backpack:ClearAllChildren()
end
end
end)
local function RoundTimer()
while wait() do
for i = intermissionlength,0,-1 do
InRound.Value = false
wait(1)
Status.Value = "Intermission: ".. i .." seconds"
end
for i = roundlength,0,-1 do
InRound.Value = true
wait(1)
Status.Value = "Game: ".. i .. " seconds"
end
end
end
spawn(RoundTimer)
This script should work, it has a table called alive that will keep track of who is alive, and if the amount of players alive is less than or equal to 1, it will end the round.
Script:
local roundlength = 90
local intermissionlength = 10
local InRound = game.ReplicatedStorage.InRound
local Status = game.ReplicatedStorage.Status
local alive = {}
local LobbySpawn = game.Workspace.LobbySpawn
local GameAreaSpawn = game.Workspace.GameAreaSpawn
InRound:GetPropertyChangedSignal("Value"):Connect(function()
if InRound.Value == true then
for _, player in pairs(game.Players:GetPlayers()) do
local char = player.Character
char.HumanoidRootPart.CFrame = GameAreaSpawn.CFrame
table.insert(alive, player.UserId)
player.Character.Humanoid.Died:Connect(function()
table.remove(alive, table.find(alive, player.UserId))
if #alive <= 1 then
InRound.Value = false
alive = {}
end
end)
end
else
for _, player in pairs(game.Players:GetPlayers()) do
local char = player.Character
char.HumanoidRootPart.CFrame = LobbySpawn.CFrame
player.Backpack:ClearAllChildren()
end
end
end)
local function RoundTimer()
while true do
for i = intermissionlength,0,-1 do
InRound.Value = false
wait(1)
Status.Value = "Intermission: ".. i .." seconds"
if i = 0 then
InRound.Value = true
alive = {}
end
end
for i = roundlength,0,-1 do
InRound.Value = true
wait(1)
Status.Value = "Game: ".. i .. " seconds"
if i = 0 then
InRound.Value = false
alive = {}
end
end
end
end
spawn(RoundTimer)
I found multiple places where I did = instead of ==. Additionally, I modified it to not use spawn as I think just a normal function call should work.
New script:
local roundlength = 90
local intermissionlength = 10
local InRound = game.ReplicatedStorage.InRound
local Status = game.ReplicatedStorage.Status
local alive = {}
local LobbySpawn = game.Workspace.LobbySpawn
local GameAreaSpawn = game.Workspace.GameAreaSpawn
InRound:GetPropertyChangedSignal("Value"):Connect(function()
if InRound.Value == true then
for _, player in pairs(game.Players:GetPlayers()) do
local char = player.Character
char.HumanoidRootPart.CFrame = GameAreaSpawn.CFrame
table.insert(alive, player.UserId)
player.Character.Humanoid.Died:Connect(function()
table.remove(alive, table.find(alive, player.UserId))
if #alive <= 1 then
InRound.Value = false
alive = {}
end
end)
end
else
for _, player in pairs(game.Players:GetPlayers()) do
local char = player.Character
char.HumanoidRootPart.CFrame = LobbySpawn.CFrame
player.Backpack:ClearAllChildren()
end
end
end)
local function RoundTimer()
while true do
for i = intermissionlength,0,-1 do
InRound.Value = false
wait(1)
Status.Value = "Intermission: ".. i .." seconds"
if i == 0 then
InRound.Value = true
alive = {}
end
end
for i = roundlength,0,-1 do
InRound.Value = true
wait(1)
Status.Value = "Game: ".. i .. " seconds"
if i == 0 then
InRound.Value = false
alive = {}
end
end
end
end
RoundTimer()