Round Scripting

Hi! So I have been working on a game with a round system with a random map choosing system and my script works fine but if you play and die one round it takes you back to the lobby and starts the countdown and puts you in another map/game. But if you die again it takes you back to the lobby but does not start the round countdown or anything again and you are just stuck in the lobby well here is the script then a picture of my Values and Value folder. Any help is appreciated :grin:
Picture:
image

Here’s the script:

local roundmsg = game.ReplicatedStorage.Values:WaitForChild("roundmsg")
local zombieCount = game.ReplicatedStorage.Values:WaitForChild("zombiesRemaining")
local zombiesAlive = game.ReplicatedStorage.Values:WaitForChild("zombiesAlive")
local gameInProgress = game.ReplicatedStorage.Values:WaitForChild("gameInProgress")
local wave = game.ReplicatedStorage.Values:WaitForChild("Wave")
local collectionservice = game:GetService("CollectionService")

while true do
	for i = 15,0,-1 do
	roundmsg.Value = "Game Starting In "..i
	if i == 1 then
		local map = math.random(1,3)
		if map == 1 then
			game.ReplicatedStorage.Maps["Nuketown Zombies"]:Clone().Parent = workspace.Map
		end
		if map == 2 then 
			game.ReplicatedStorage.Maps["Rundown hosipital area"]:Clone().Parent = workspace.Map
		end
		if map == 3 then
			game.ReplicatedStorage.Maps["Town Map"]:Clone().Parent = workspace.Map
		end
		print(map)
		for i, v in pairs(game.Players:GetPlayers()) do
			collectionservice:AddTag(v.Character, "Alive")
			game.Workspace:WaitForChild(v.Name)
			v.Character:WaitForChild("HumanoidRootPart") 
				 v.Character.HumanoidRootPart.CFrame = workspace.Map:FindFirstChildOfClass("Model").Spawn.CFrame
			if not v.Character then return 
			end
			roundmsg.Value = "Game In Progress"
			zombieCount.Value = 6
			zombiesAlive.Value = 6
			wave.Value = 1
			gameInProgress.Value = true
			
			repeat
				if #collectionservice:GetTagged("Alive") > 0 then
					if zombiesAlive.Value == 0 then
						wave.Value  = wave.Value + 1
						zombieCount.Value = 6 * wave.Value
						zombiesAlive.Value = 6 * wave.Value
					end
				elseif wave.Value >= 1 and #collectionservice:GetTagged("Alive") == 0 then
					gameInProgress = false
				end
				wait(1)
			until gameInProgress == false
				workspace.Map:ClearAllChildren()
		end
	end
	wait(1)
end
end
2 Likes

I’m not very good with this stuff, but you can use functions. Put everything into a function, and at the end of the script, run the function? You’ll probably have to do a-lot of tweaking.

{local} function functionName()
[Your script here]
functionName()

That should do it. The local is used only if you won’t use that same name in another script, obviously. Just slap the functionName() after the ends on your script. It will probably re-do the script forever. If not, just keep the while statement. It looks like you have it, though, maybe someone else will explain the problem.

Edit: In Linux, all you have to do is do ./[filename] and put it in [filename]. Maybe Roblox will add a feature like that, or has one?

1 Like

So do something like this? (I know some stuff might be wrong but I just threw stuff together real quick)

local roundmsg = game.ReplicatedStorage.Values:WaitForChild("roundmsg")
local zombieCount = game.ReplicatedStorage.Values:WaitForChild("zombiesRemaining")
local zombiesAlive = game.ReplicatedStorage.Values:WaitForChild("zombiesAlive")
local gameInProgress = game.ReplicatedStorage.Values:WaitForChild("gameInProgress")
local wave = game.ReplicatedStorage.Values:WaitForChild("Wave")
local collectionservice = game:GetService("CollectionService")

while true do
	for i = 15,0,-1 do
	roundmsg.Value = "Game Starting In "..i
	if i == 1 then
		local map = math.random(1,3)
		if map == 1 then
			game.ReplicatedStorage.Maps["Nuketown Zombies"]:Clone().Parent = workspace.Map
		end
		if map == 2 then 
			game.ReplicatedStorage.Maps["Rundown hosipital area"]:Clone().Parent = workspace.Map
		end
		if map == 3 then
			game.ReplicatedStorage.Maps["Town Map"]:Clone().Parent = workspace.Map
		end
			print(map)
			local function addPlayer()
				for i, v in pairs(game.Players:GetPlayers()) do
					collectionservice:AddTag(v.Character, "Alive")
					game.Workspace:WaitForChild(v.Name)
					v.Character:WaitForChild("HumanoidRootPart") 
					v.Character.HumanoidRootPart.CFrame = workspace.Map:FindFirstChildOfClass("Model").Spawn.CFrame
					if not v.Character then return 
					end
					roundmsg.Value = "Game In Progress"
					zombieCount.Value = 6
					zombiesAlive.Value = 6
					wave.Value = 1
					gameInProgress.Value = true
				end
	
				local function newWave()
					
			repeat
				if #collectionservice:GetTagged("Alive") > 0 then
					if zombiesAlive.Value == 0 then
						wave.Value  = wave.Value + 1
						zombieCount.Value = 6 * wave.Value
						zombiesAlive.Value = 6 * wave.Value
						end
						end
						local function died()
							if #collectionservice:GetTagged("Alive") == 0 then
								gameInProgress = false
							end
						end
	
						local function cleanUp()
							if gameInProgress == false then
								workspace.Map:ClearAllChildren()
								end
2 Likes

functionName() Runs the function, while local function functionName() makes one. I don’t want to break your script, but I think it would look like this?

local roundmsg = game.ReplicatedStorage.Values:WaitForChild("roundmsg")
local zombieCount = game.ReplicatedStorage.Values:WaitForChild("zombiesRemaining")
local zombiesAlive = game.ReplicatedStorage.Values:WaitForChild("zombiesAlive")
local gameInProgress = game.ReplicatedStorage.Values:WaitForChild("gameInProgress")
local wave = game.ReplicatedStorage.Values:WaitForChild("Wave")
local collectionservice = game:GetService("CollectionService")

local function whileReplacement()
	for i = 15,0,-1 do
	roundmsg.Value = "Game Starting In "..i
	if i == 1 then
		local map = math.random(1,3)
		if map == 1 then
			game.ReplicatedStorage.Maps["Nuketown Zombies"]:Clone().Parent = workspace.Map
		end
		if map == 2 then 
			game.ReplicatedStorage.Maps["Rundown hosipital area"]:Clone().Parent = workspace.Map
		end
		if map == 3 then
			game.ReplicatedStorage.Maps["Town Map"]:Clone().Parent = workspace.Map
		end
			print(map)
			local function addPlayer()
				for i, v in pairs(game.Players:GetPlayers()) do
					collectionservice:AddTag(v.Character, "Alive")
					game.Workspace:WaitForChild(v.Name)
					v.Character:WaitForChild("HumanoidRootPart") 
					v.Character.HumanoidRootPart.CFrame = workspace.Map:FindFirstChildOfClass("Model").Spawn.CFrame
					if not v.Character then return 
					end
					roundmsg.Value = "Game In Progress"
					zombieCount.Value = 6
					zombiesAlive.Value = 6
					wave.Value = 1
					gameInProgress.Value = true
				end
				newWave()
				addPlayer()
			repeat
				if #collectionservice:GetTagged("Alive") > 0 then
					if zombiesAlive.Value == 0 then
						wave.Value  = wave.Value + 1
						zombieCount.Value = 6 * wave.Value
						zombiesAlive.Value = 6 * wave.Value
						end
						end
						local function died()
							if #collectionservice:GetTagged("Alive") == 0 then
								gameInProgress = false
                                                                cleanup()
							end
						end
	
						local function cleanUp()
							if gameInProgress == false then
								workspace.Map:ClearAllChildren()
                                wait(1)
                                whileReplacement()
								end

Sorry if I botched your script, this text editor is not my favorite script editor.

Edit: I put whileReplacement() in the cleanup function to restart the script. It does the function again after cleanup.

Another Edit, as I am too quick to posting: If you want a much more organized script, take some parts out, and remove the locals. Or do some sort of parent-child system. Then, you can call the function from anywhere. Just add the same locals from the top of the script.

2 Likes

I got this error message
ServerScriptService.Script:60: Expected ‘end’ (to close ‘function’ at line 55), got ; did you forget to close ‘then’ at line 56?

2 Likes

Oh, just align the end with the if.

2 Likes

Now I just got this since I fixed it
ServerScriptService.Script:64: Expected ‘until’ (to close ‘repeat’ at line 40), got ; did you forget to close ‘then’ at line 57?

2 Likes

You forgot to make an until statement after your repeat statement. Repeat until what is what? Repeat until something = true/false? I haven’t grasped the script very well, so you’ll need to put your until statement somewhere you think it’ll fit.

2 Likes

Everywhere that I have put it the script has ended up erroring I have no clue where to put it

2 Likes

I think that it will work since I added a until wave.Value == 100000
(hopefully)
But then I got another error being
ServerScriptService.Script:70: Expected ‘end’ (to close ‘then’ at line 11), got ; did you forget to close ‘then’ at line 60?

2 Likes

Ok So I patched it up and now I am getting this error
17:03:27.160 ServerScriptService.Script:43: attempt to call a nil value - Server - Script:43
17:03:27.160 Stack Begin - Studio
17:03:27.161 Script ‘ServerScriptService.Script’, Line 43 - Studio - Script:43
17:03:27.161 Stack End

2 Likes

justdeleteit

I am one of those people who find the least convenient way to reply to something, taking about an hour to do so.

1 Like

Ok so I did that now what should I do about the error?

2 Likes

On 43, you have a bad value, apparently. What is on 43?

1 Like

It is newWave()
(characterscharacters)
I actually moved it up some but it still errors but it says on line 41 now Its the same thing though

2 Likes

I moved the part that had the repeat loop and and gave it the newWave() function but now when I log in to the game there is no errors but It does not start the countdown for a new round and then start a new round so what can I do?

2 Likes

Stick this at the very end of the script, past all the ends :

whileReplacement()
2 Likes

I just did that and still nothing happened

2 Likes

I have a similar script to yours, I used it before.

Maybe it can help you.

local roundMsg = game.ReplicatedStorage.Values:WaitForChild("roundmsg")
local zombieCount = game.ReplicatedStorage.Values:WaitForChild("zombiesRemaining")
local zombiesAlive = game.ReplicatedStorage.Values:WaitForChild("zombiesAlive")
local gameInProgress = game.ReplicatedStorage.Values:WaitForChild("gameInProgress")
local wave = game.ReplicatedStorage.Values:WaitForChild("Wave")
local collectionservice = game:GetService("CollectionService")


while true do
	for i = 15,0,-1 do
		roundMsg.Value = "Game Starting in: "..i
		if i == 1 then
			local map = math.random(1,3)
			if map == 1 then
				game.ReplicatedStorage.Maps.Map1:Clone().Parent = workspace.Map
			end
			if map == 2 then
				game.ReplicatedStorage.Maps.Map2:Clone().Parent = workspace.Map
			end
			if map == 3 then
				game.ReplicatedStorage.Maps.Map3:Clone().Parent = workspace.Map
			end
			
			for i, v in pairs(workspace.Map:FindFirstChildOfClass("Model"):GetChildren()) do
				if v.Name == "spawner" then
					collectionservice:AddTag(v, "spawner")
				end
			end
			for i, v in pairs(game.Players:GetPlayers()) do
				collectionservice:AddTag(v.Character, "Alive")
				v.Character.HumanoidRootPart.CFrame = workspace.Map:FindFirstChildOfClass("Model").Spawn.CFrame
			end
			roundMsg.Value = "Game In Progress"
			zombieCount.Value = 6
			zombiesAlive.Value = 6
			wave.Value = 1
			gameInProgress.Value = true
			repeat
				if #collectionservice:GetTagged("Alive") > 0 then
					if zombiesAlive.Value == 0 then
						wave.Value = wave.Value + 1
						zombieCount.Value = 6 * wave.Value
						zombiesAlive.Value = 6 * wave.Value
					end
					elseif #collectionservice:GetTagged("Alive") == 0 then
					gameInProgress.Value = false
				end
				wait(1)
			until gameInProgress.Value == false
				workspace.Map:ClearAllChildren()
			end
	wait(1)
	end
	end

Hope this helps you. If you see a mistake in your output, try to check if the script I provided you has the correct names and instances!

3 Likes

Looks like we have someone who knows what they’re doing.

1 Like