Round Timer game with randomly chosen Killer and Survivors script help needed

  • What does the code do and what are you not satisfied with?
    There are two separate working codes
    One code randomly chooses a Killer and Survivors in my round based timer game.

The other code has a round timer with a lobby time and a round duration time.

The two scripts are not working together in a fashion that is suitable. For the very first round a Killer and Survivor are chosen successfully, however when the next round starts the Random Generator script does not re-choose a new Killer and Survivor. I put a wait(35) into the Random Generator script b/c it needs to wait to choose until they get into the mapCopy, I’ve found that if the Random Generator choses before players are teleported it tries to choose again and then it might make all players Survivors or all players Killers and does not end up working so I fixed that by making sure it didn’t chose until the players were actually teleported into the game round.

  • What potential improvements have you considered?
    Instead of having a separate script for the Random Generator choosing Killer/Survivors I tried to incorporate it into my Lobby/Game Round Timer Script. However being less experienced at writing script I have not been able to successfully figure this out yet. I am ok with keeping two different scripts yet I am not sure what to change in the Random Generator script to make sure it re-chooses once a new round starts.

Random Generator Killer/Survivor Script

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

while wait(35) do
	pickPlayers()
end

Round Based Timer Lobby/Game Round Script

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	
			

					

				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)

Never Mind… I finally got it to work by combining it into my timer script like so…

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)
1 Like