Lobby Intermission & In Game timer script

I need help with some code that is currently operational and working in the game. It is a code that has a lobby intermission of 20 seconds upon entering the game. After the intermission time is up, the player is transported into the game round of 20 minutes. If the player dies they are transported back to the lobby and they have to wait out the remaining time in the lobby. The lobby is a room on the same map as the in game, but it is closed off so the players have to wait to be transported. My code is sitting in Server Script Service.

I am very new at coding and scripting and trying to get the hang of it. I watch videos and do some self teaching but I seem to be having a difficult time learning it. I feel like I am not really grasping and getting the hang of the code in a fashion that I can sit down and write my own code. I want to move away from the feeling that I’m just copying script and more into I am understanding it and can think to write my own.

*** What does the code do and what are you not satisfied with?** This code creates a Lobby Intermission of 20 seconds before being transported in game. The in game has a timer of 20 minutes. If the player dies, they get transported back to the lobby to wait out the remaining time left on the timer before being transported in game again.
*** What potential improvements have you considered?** I do not want a player who dies early on in the game to wait so long in the lobby. I feel its too long to wait it out.
*** How (specifically) do you want to improve the code?** I’d like my code to allow 3 deaths in game before they are transported back to the lobby to wait it out for the current game cycle to finish.

This is the code I am using. I don’t know what to do to include allowing 3 deaths before being transported back to the lobby. I could just remove the lobby all together and allow unlimited deaths but then I don’t feel the game has much of a challenge. On the other hand if a player dies early in the game, having to wait too long they will likely lose interest in staying put for another round.

local lobbyLocation = game.Workspace.Lobby.Position + Vector3.new(0,3,0)

local gameLocation = game.Workspace.Main.Position + Vector3.new(0,3,0)



local ReplicatedStorage = game:GetService('ReplicatedStorage')

local timeEvent = ReplicatedStorage:WaitForChild('TimeEvent')



local function playGame()

	local timeAmount = 1200

	local timerText = 'Remaining Time: '

	while timeAmount > 0 do

		timeEvent:FireAllClients(timeAmount, timerText)

		wait(1)

		timeAmount -= 1

	end

end



local function playIntermission()

	local intermission = 20

	local timerText = 'Intermission: '

	while intermission > 0 do

		timeEvent:FireAllClients(intermission, timerText)

		wait(1)

		intermission -= 1

	end

end



local function resetPlayers()

	for _, plr in pairs(game.Players:GetChildren()) do

		plr.Character.HumanoidRootPart.CFrame = CFrame.new(lobbyLocation)

	end 

end



local function teleportPlayers()

	for _, plr in pairs(game.Players:GetChildren()) do

		plr.Character.HumanoidRootPart.CFrame = CFrame.new(gameLocation)

	end 

end



while true do

	resetPlayers()

	playIntermission()

	teleportPlayers()

	playGame()

end

I’m lost. Would this go into my current script or does it go under Players as a script. I tried both and neither worked.

Thx

Hello there, what you could do is make an IntValue of 3 IntValue | Documentation - Roblox Creator Hub, which whenever death happens they will have their IntValue decrease by one, you can do this by firstly creating an IntValue which isn’t that hard and then use Humanoid | Documentation - Roblox Creator Hub to check which player has died thus you can change the value to decrease by 1, furthermore having a check for if the IntValue == 0 or not whenever a player has died, make sure to have the decrease by 1 before the check.

Also if the IntValue is greater than 1 then you can LoadCharacter on the player and set their position to the map LoadCharacter at a specific position

I appreciate the responses and help, I just still do not know how to do your suggestions and that is the issue. I’m not grasping.

First of all, you need to create a new server script which will assign each player an Integer Value, which determines how many deaths they currently have:

local function newPlayer(player)
    local Deaths = Instance.new("IntValue", player) -- creates an Int Value parented by the new player
    Deaths.Name = "Deaths" 
    Deaths.Value = 0 -- ensures the value starts at 0 
end

game.Players.PlayerAdded:Connect(newPlayer) -- runs the function above whenever a new player connects

Next, you will need to update your current script that you posted with this:

local lobbyLocation = game.Workspace.Lobby.Position + Vector3.new(0,3,0)

local gameLocation = game.Workspace.Main.Position + Vector3.new(0,3,0)



local ReplicatedStorage = game:GetService('ReplicatedStorage')

local timeEvent = ReplicatedStorage:WaitForChild('TimeEvent')

local function playerDied(player)
	if player:FindFirstChild("Deaths") then
		local deaths = player.Deaths
		deaths.Value = deaths.Value + 1

        if deaths.Value < 3 then 
            player.Character.HumanoidRootPart.CFrame = CFrame.new(gameLocation) 
        end
	end
end


local function playGame()

	local timeAmount = 1200

	local timerText = 'Remaining Time: '
	
	for _,player in pairs(game.Players:GetChildren()) do 
		if player:FindFirstChild("Deaths") then
			player.Deaths.Value = 0 -- loops through the player's death value and resets it to 0 before the round starts
		end
	end
	
	while timeAmount > 0 do

		timeEvent:FireAllClients(timeAmount, timerText)

		wait(1)
		
		for _,player in pairs(game.Players:GetChildren()) do
			if player:FindFirstChild("Deaths") then
				if player.Deaths.Value > 2 then
					player.Character.HumanoidRootPart.CFrame = CFrame.new(lobbyLocation) -- loops through players during round and respawns if 3 deaths
				end
			end
		end
		
		timeAmount -= 1

	end

end



local function playIntermission()

	local intermission = 20

	local timerText = 'Intermission: '

	while intermission > 0 do

		timeEvent:FireAllClients(intermission, timerText)

		wait(1)

		intermission -= 1

	end

end



local function resetPlayers()

	for _, plr in pairs(game.Players:GetChildren()) do

		plr.Character.HumanoidRootPart.CFrame = CFrame.new(lobbyLocation)

	end 

end



local function teleportPlayers()

	for _, plr in pairs(game.Players:GetChildren()) do

		plr.Character.HumanoidRootPart.CFrame = CFrame.new(gameLocation)

	end 

end

game.Players.PlayerAdded:Connect(function(player)
	local character = player.Character
	local humanoid = character:FindFirstChild("Humanoid")
	
	if humanoid then
		humanoid.Died:Connect(playerDied)
	end
end)


while true do

	resetPlayers()

	playIntermission()

	teleportPlayers()

	playGame()

end

The update detects if the player’s character dies, adding a death when it does. The updated function will detect when the deaths reach 3 and then respawn the player back to the lobby.

3 Likes