How to adapt current game timer/teleport system?

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

  1. If nobody is left
  2. If only one person is left
  3. 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!

1 Like

Please format your code in code blocks, see example below:

-- Example Lua code block, using ```lua before the block and ``` after (Put the formatting marks on a separate line from the code) .

Oh sorry, I was trying to figure out that.

1 Like

Additionally, could I see the explorer? I don’t know what class the objects are when I can’t see the explorer.

i love using kbd

This shows everything referenced in the script except the two spawns.

1 Like

I have a question. I was just looking through your script and why are you killing the player after teleporting them? (See image below)

Also, I am having trouble understanding what you are trying to accomplish.

Okay.

I don’t understand this. Is this a current problem in the game? Or intended gameplay?

Do you mean, “After the game ends, the game instantly restarts”?

Sounds good.

This is a problem with it, correct?

Got it.

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.

You could instead use Player.Backpack:ClearAllChildren()

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.

Yea

Yea that would work better, I’ll use that.

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)

Oh thank you bro. Do you have any pointers on how to end the game

?

Yes, I will begin working on that now.

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 put an extra equals sign on your if i = 0 then, and tested it in studio with three fake people. It functioned that same as the old one except

  1. When you would die, it would teleport all your dead body parts into the lobby and then respawn you into the lobby
  2. When someone would die, it would teleport everybody (everybody except the person who died) to the lobby, and then back to the gameplay area.

Hmm, let me look into it in that case.

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()

Try again maybe?

I tried it out, but all it did different from the original script was

  1. When you would die, it would teleport all your dead body parts into the lobby and then respawn you into the lobby

  2. When someone would die, everybody excluding the person who died who be respawned back in the game area