Why does this ignore the script?

Hey devs!


Sorry if I seem dumb here, I’m very tired and just got out from a tornado warning, kind of frightened and delirious right now, but:

I had a round script that worked 100% fine yesterday and most of today until this past 2 hours. It suddenly broke in the round loop and ignored all of the round code and jumps right back to the intermission after loading the map.

I’ve tried moving code around, changing code entirely, but ended up reverting it back to the old code. I can’t seem to fix it.

It is inside of a module script that has a server script run everything important for the game and leaderstats (leaderstats aren’t important for this).


Entire Server Code:

--Change CFrame value for tools: game.StarterGui.MainGui.Frames.Shop.Frames.Items.Purchase.ItemViewport.Blank.CameraCFrame.Value = game.Workspace.CurrentCamera.CFrame

local Players = game:GetService("Players")
local replicatedStorage = game:GetService("ReplicatedStorage")
local serverStorage = game:GetService("ServerStorage")
local marketPlaceService = game:GetService("MarketplaceService")

local mainGame = replicatedStorage:WaitForChild("MainGame")
local remotes = mainGame:WaitForChild("Remotes")
local gameValues = mainGame:WaitForChild("GameValues")

local status = gameValues:WaitForChild("Status")

local round = require(script.Round)
local leaderstats = require(script.Leaderstats)

local bindableEvent = Instance.new("BindableEvent")
bindableEvent.Name = "BindableEvent"
bindableEvent.Parent = remotes

local minimumPlayers = 2
local intermissionTime = 20
local gameLength = 180

local vipGamePass = 42960558
local extraSpeedGamePass = 43113220

local playersLeft = 0

for _, object in pairs(workspace.Camera:GetChildren()) do
	object:Destroy()
end

Players.PlayerAdded:Connect(function(player)
	leaderstats.LoadChatTags(player)
	leaderstats.LoadLeaderstats(player)
	
	playersLeft = playersLeft + 1
end)

Players.PlayerRemoving:Connect(function(player)
	leaderstats.SaveData(player)
	
	playersLeft = playersLeft - 1
end)


while true do
	round.Waiting(Players.NumPlayers)
	
	repeat task.wait(1) until Players.NumPlayers >= minimumPlayers
	
	round.Intermission(intermissionTime)
	
	round.StartRound(gameLength)
	
	task.wait(3)
end

game:BindToClose(function()
	while playersLeft > 0 do
		bindableEvent.Event:Wait()
	end
end)

Entire Module Code:

local Players = game:GetService("Players")
local replicatedStorage = game:GetService("ReplicatedStorage")
local lighting = game:GetService("Lighting")
local tweenService = game:GetService("TweenService")
local serverStorage = game:GetService("ServerStorage")
local marketPlaceService = game:GetService("MarketplaceService")

local maps = serverStorage:WaitForChild("MainGame"):WaitForChild("Maps")

local mainGame = replicatedStorage:WaitForChild("MainGame")
local remotes = mainGame:WaitForChild("Remotes")
local gameValues = mainGame:WaitForChild("GameValues")

local status = gameValues:WaitForChild("Status")

local vipGamePass = 42960558
local tripleExpPass = 43694425

local cashReward = 75
local expReward = 75

local function toMS(s)
	return ("%02i:%02i"):format(s/60%60, s%60)
end

local round = {}

function round.Waiting(numberOfPlayers)
	gameValues:WaitForChild("Status").Value = "2 Ready players are required to start ("..numberOfPlayers.."/2 Players)"
end

function round.Intermission(intermissionTime)
	for i=intermissionTime,0,-1 do
		status.Value = "Intermission ("..i..")"
		task.wait(1)
	end
	
end

function round.StartRound(gameLength)
	status.Value = ""
	
	local plrs = {}

	for i, player in pairs(Players:GetPlayers()) do
		if player then
			table.insert(plrs,player)
		end
	end
	
	task.wait(2)
	
	local availableMaps = maps:GetChildren()
	local chosenMap = availableMaps[math.random(1,#availableMaps)]
	
	status.Value = chosenMap.Name.." was selected"
	
	task.wait(2)
	
	local newMap = chosenMap:Clone()
	newMap.Name = chosenMap.Name
	newMap.Parent = workspace
	
	local spawnPoints = newMap:FindFirstChild("SpawnPoints")
	
	if not spawnPoints then
		warn("Critical Error: There are no spawn points in "..chosenMap.Name.."!")
	end
	
	local availableSpawns = spawnPoints:GetChildren()
	
	for i=5,0,-1 do
		status.Value = "Game will start in "..i
		task.wait()
	end
	
	newMap.Music:Play()
	
	for i, plr in pairs(plrs) do
		if plr then
			local character = plr.Character or plr.CharacterAdded:Wait()
			
			if character then
				task.wait(1.2)
				
				character.Humanoid.Sit = false
				character.HumanoidRootPart.Position = availableSpawns[1].Position + Vector3.new(0, 8, 0)
				table.remove(availableSpawns,1)
				
				local hitbox = serverStorage.MainGame.Templates.Hitbox:Clone()
				hitbox.Parent = plr.Character
				hitbox.CFrame = plr.Character.HumanoidRootPart.CFrame
				
				local weld = Instance.new("Weld")
				weld.Parent = hitbox
				weld.Part0 = hitbox
				weld.Part1 = plr.Character.HumanoidRootPart
				
				local swordClone = serverStorage.MainGame.Tools.ClassicSword:Clone()
				swordClone.Parent = plr.Backpack
				
				local equipped = serverStorage.MainGame.PlayerData[plr.Name].Equipped
				
				if equipped.Value ~= "" then
					local weapon = serverStorage.MainGame.Tools.ShopTools[equipped.Value]:Clone()
					weapon.Parent = plr.Backpack
				else
					return
				end
				
				local gameTag = Instance.new("BoolValue")
				gameTag.Name = "GameTag"
				gameTag.Parent = plr.Character
			end
		else
			if not plr then
				table.remove(plrs,i)
			end
		end
		
	end
	
	for i=gameLength,0,-1 do
		for x, player in pairs(plrs) do
			if player then
				local character = player.Character

				if not character then
					table.remove(plrs,x)
				else
					if character:FindFirstChild("GameTag") then
						--Still in game
					else
						table.remove(plrs,x)
						player.leaderstats.Cash.Value += 5
						plrs[1].leaderstats.Level.CurrentExp.Value += 10
					end
				end
			else
				table.remove(plrs,x)
			end
			
		end
		
		status.Value = "Time: "..toMS(i).." remaining"
		
		if #plrs == 1 then
			status.Value = "Cleaning Up: The winner is "..plrs[1].Name..". Congrats!"
			
			if marketPlaceService:UserOwnsGamePassAsync(plrs[1].UserId, vipGamePass) then
				plrs[1].leaderstats.Cash.Value += (cashReward * 0.2 + cashReward)
				plrs[1].leaderstats.Level.CurrentExp.Value += (expReward * 0.2 + expReward)
			elseif marketPlaceService:UserOwnsGamePassAsync(plrs[1].UserId, tripleExpPass) then
				plrs[1].leaderstats.Cash.Value += cashReward
				plrs[1].leaderstats.Level.CurrentExp.Value += (expReward * 3)
			elseif marketPlaceService:UserOwnsGamePassAsync(plrs[1].UserId, vipGamePass) and marketPlaceService:UserOwnsGamePassAsync(plrs[1].UserId, tripleExpPass) then
				plrs[1].leaderstats.Cash.Value += (cashReward * 0.2 + cashReward)
				plrs[1].leaderstats.Level.CurrentExp.Value += (expReward * 0.2 + expReward * 3)
			else
				plrs[1].leaderstats.Cash.Value += cashReward
				plrs[1].leaderstats.Level.CurrentExp.Value += expReward
			end
			
			break
		elseif #plrs == 0 then
			status.Value = "Restarting: No one has won the game."
			break
		elseif i == 0 then
			status.Value = "Restarting: Time has run out."
			break
		end

		task.wait(1)
	end
	
	task.wait(1.5)
	
	for i, player in pairs(Players:GetPlayers()) do
		local character = player.Character

		if not character then
			--Ignore this player
		else
			if character:FindFirstChild("GameTag") then
				character.GameTag:Destroy()
			end

			for _, tool in pairs(player.Backpack:GetChildren()) do
				if tool:FindFirstChild("Price") then
					tool:Destroy()
				end
			end
			
			for _, tool in pairs(character:GetChildren()) do
				if tool:FindFirstChild("Price") then
					tool:Destroy()
				end
			end

		end

		player:LoadCharacter()
	end
	
	status.Value = "Cleaning up last round"
	
	task.wait(3)
	
	newMap:Destroy()
	
	task.wait(7)
end

return round

Sorry if the code is a lot to read!


I can be a real doofus sometimes, so any help is appreciated! Thanks!

I genuinely don’t know what broke, before there was a tornado it was fine, afterwards code broke out of no where.

Best regards,
RoboBoy013

2 Likes

Well, I see 3 break statements and they would only happen if there is one player, no players, or the time has ran out. Did you test the code with more than one person/client connected? I see no other reason why the code would break like that.

Also, make sure to add 1 second to wait in task.wait.

If there were any errors in the console from your script, please take a screenshot and send it here. I’m not sure if module scripts show errors but if you truly think it’s an error from one of the two scripts but nothing shows, it could possibly be coming from the module script. You will just have to look into it.

Sorry if this doesn’t help, but I’m not sure what else would be the problem unless the module script function is somehow being spawned like how task.spawn works and then gets skipped and overexecuted.

1 Like

Oh, well the task.wait() explains why the countdown didn’t work. But the rest of the code worked fine after it. My “1” key is kinda faulty, it will press twice or not register at all 80% of the time.

Rest of the code still doesn’t work because it breaks the loop and moves on with no error, and the game will not run without 2 players, so I have been testing with 2 players. Both in test mode, in game with me and my alt and in game with friends.

Thanks for the help anyways, is greatly appreciated!

I went and copied all the code to the server script to find any possible console errors, and nothing showed up. I’ve reviewed the code multiple times and nothing seems wrong with it.

I’ll review it more later, but thanks for your help!

I found some mistakes made within the module script. Not sure if this has to do with the game skipping after loading the map, but I saw that when you were looping through the players in this function you used the return function if the player didn’t have an item equipped. Returning will just return a value to the whole function that you executed. Instead, use the keyword continue. This keyword will just skip to the next item in the table without executing any further code.

Edit: I’m editing this part of the post because you do not need to continue through the table of players unless it has no coroutine. But if it has no coroutine, then continue will be useful in some cases.

Also, I saw that you waited 1.2 seconds for every player. This is a problem because, for instance, if there are 20 people in your game, it will wait 1.2 seconds for each player which is approximately 24 seconds to start the round. The solution to this is coroutines. Coroutines are similar to task.spawn but in my opinion are faster. What these 2 functions do is basically let the script perform multiple tasks at a time. This will help so you don’t have to wait 1.2 seconds for each player, it will just spawn the wanted code in for each player.

In the ServerScript, I found a problem with this part. You’re creating a while loop that loops until true is false, but true is always true. The point is, the while loop will not allow you to execute any further code until the loop is done. You can use coroutines to put the while loop into. If you prefer otherwise, you can just refrain from putting any code after the while loop. In this case, just swap the BindToClose with the while loop.

I’ve seen many things wrong with these 2 scripts, so I’ve decided to fix them myself. Here are the new scripts that I came up with.

Scripts
Server Script
--Change CFrame value for tools: game.StarterGui.MainGui.Frames.Shop.Frames.Items.Purchase.ItemViewport.Blank.CameraCFrame.Value = game.Workspace.CurrentCamera.CFrame

local Players = game:GetService("Players")
local replicatedStorage = game:GetService("ReplicatedStorage")
local serverStorage = game:GetService("ServerStorage")
local marketPlaceService = game:GetService("MarketplaceService")

local mainGame = replicatedStorage:WaitForChild("MainGame")
local remotes = mainGame:WaitForChild("Remotes")
local gameValues = mainGame:WaitForChild("GameValues")

local status = gameValues:WaitForChild("Status")

local round = require(script.Round)
local leaderstats = require(script.Leaderstats)

local bindableEvent = Instance.new("BindableEvent")
bindableEvent.Name = "BindableEvent"
bindableEvent.Parent = remotes

local minimumPlayers = 2
local intermissionTime = 20
local gameLength = 180

local vipGamePass = 42960558
local extraSpeedGamePass = 43113220

local playersLeft = 0

for _, object in pairs(workspace.Camera:GetChildren()) do
	object:Destroy()
end

Players.PlayerAdded:Connect(function(player)
	leaderstats.LoadChatTags(player)
	leaderstats.LoadLeaderstats(player)

	playersLeft = playersLeft + 1
end)

Players.PlayerRemoving:Connect(function(player)
	leaderstats.SaveData(player)

	playersLeft = playersLeft - 1
end)


coroutine.resume(coroutine.create(function() 
	while true do
		round.Waiting(Players.NumPlayers)

		repeat task.wait(1) until Players.NumPlayers >= minimumPlayers

		round.Intermission(intermissionTime)

		round.StartRound(gameLength)

		task.wait(3)
	end
end))();

game:BindToClose(function()
	while playersLeft > 0 do
		bindableEvent.Event:Wait()
	end
end)
Module Script
local Players = game:GetService("Players")
local replicatedStorage = game:GetService("ReplicatedStorage")
local lighting = game:GetService("Lighting")
local tweenService = game:GetService("TweenService")
local serverStorage = game:GetService("ServerStorage")
local marketPlaceService = game:GetService("MarketplaceService")

local maps = serverStorage:WaitForChild("MainGame"):WaitForChild("Maps")

local mainGame = replicatedStorage:WaitForChild("MainGame")
local remotes = mainGame:WaitForChild("Remotes")
local gameValues = mainGame:WaitForChild("GameValues")

local status = gameValues:WaitForChild("Status")

local vipGamePass = 42960558
local tripleExpPass = 43694425

local cashReward = 75
local expReward = 75

local function toMS(s)
	return ("%02i:%02i"):format(s/60%60, s%60)
end

local round = {}

function round.Waiting(numberOfPlayers)
	gameValues:WaitForChild("Status").Value = "2 Ready players are required to start ("..numberOfPlayers.."/2 Players)"
end

function round.Intermission(intermissionTime)
	for i=intermissionTime,0,-1 do
		status.Value = "Intermission ("..i..")"
		task.wait(1)
	end

end

function round.StartRound(gameLength)
	status.Value = ""

	local plrs = {}

	for i, player in pairs(Players:GetPlayers()) do
		if player then
			table.insert(plrs,player)
		end
	end

	task.wait(2)

	local availableMaps = maps:GetChildren()
	local chosenMap = availableMaps[math.random(1,#availableMaps)]

	status.Value = chosenMap.Name.." was selected"

	task.wait(2)

	local newMap = chosenMap:Clone()
	newMap.Name = chosenMap.Name
	newMap.Parent = workspace

	local spawnPoints = newMap:FindFirstChild("SpawnPoints")

	if not spawnPoints then
		warn("Critical Error: There are no spawn points in "..chosenMap.Name.."!")
	end

	local availableSpawns = spawnPoints:GetChildren()

	for i=5,0,-1 do
		status.Value = "Game will start in "..i
		task.wait(1)
	end

	newMap.Music:Play()

	for i, plr in pairs(plrs) do
		if plr then
			local character = plr.Character or plr.CharacterAdded:Wait()

			if character then
				coroutine.resume(coroutine.create(function()
					task.wait(1.2)

					character.Humanoid.Sit = false
					character.HumanoidRootPart.Position = availableSpawns[1].Position + Vector3.new(0, 8, 0)
					table.remove(availableSpawns,1)

					local hitbox = serverStorage.MainGame.Templates.Hitbox:Clone()
					hitbox.Parent = plr.Character
					hitbox.CFrame = plr.Character.HumanoidRootPart.CFrame

					local weld = Instance.new("Weld")
					weld.Parent = hitbox
					weld.Part0 = hitbox
					weld.Part1 = plr.Character.HumanoidRootPart

					local swordClone = serverStorage.MainGame.Tools.ClassicSword:Clone()
					swordClone.Parent = plr.Backpack

					local equipped = serverStorage.MainGame.PlayerData[plr.Name].Equipped

					if equipped.Value ~= "" then
						local weapon = serverStorage.MainGame.Tools.ShopTools[equipped.Value]:Clone()
						weapon.Parent = plr.Backpack
					end

					local gameTag = Instance.new("BoolValue")
					gameTag.Name = "GameTag"
					gameTag.Parent = plr.Character
				end));
			end
		else
			if not plr then
				table.remove(plrs,i)
			end
		end

	end

	for i=gameLength,0,-1 do
		for x, player in pairs(plrs) do
			if player then
				local character = player.Character

				if not character then
					table.remove(plrs,x)
				else
					if character:FindFirstChild("GameTag") then
						--Still in game
					else
						table.remove(plrs,x)
						player.leaderstats.Cash.Value += 5
						plrs[1].leaderstats.Level.CurrentExp.Value += 10
					end
				end
			else
				table.remove(plrs,x)
			end

		end

		status.Value = "Time: "..toMS(i).." remaining"

		if #plrs == 1 then
			status.Value = "Cleaning Up: The winner is "..plrs[1].Name..". Congrats!"

			if marketPlaceService:UserOwnsGamePassAsync(plrs[1].UserId, vipGamePass) then
				plrs[1].leaderstats.Cash.Value += (cashReward * 0.2 + cashReward)
				plrs[1].leaderstats.Level.CurrentExp.Value += (expReward * 0.2 + expReward)
			elseif marketPlaceService:UserOwnsGamePassAsync(plrs[1].UserId, tripleExpPass) then
				plrs[1].leaderstats.Cash.Value += cashReward
				plrs[1].leaderstats.Level.CurrentExp.Value += (expReward * 3)
			elseif marketPlaceService:UserOwnsGamePassAsync(plrs[1].UserId, vipGamePass) and marketPlaceService:UserOwnsGamePassAsync(plrs[1].UserId, tripleExpPass) then
				plrs[1].leaderstats.Cash.Value += (cashReward * 0.2 + cashReward)
				plrs[1].leaderstats.Level.CurrentExp.Value += (expReward * 0.2 + expReward * 3)
			else
				plrs[1].leaderstats.Cash.Value += cashReward
				plrs[1].leaderstats.Level.CurrentExp.Value += expReward
			end

			break
		elseif #plrs == 0 then
			status.Value = "Restarting: No one has won the game."
			break
		elseif i == 0 then
			status.Value = "Restarting: Time has run out."
			break
		end

		task.wait(1)
	end

	task.wait(1.5)

	for i, player in pairs(Players:GetPlayers()) do
		local character = player.Character

		if not character then
			--Ignore this player
		else
			if character:FindFirstChild("GameTag") then
				character.GameTag:Destroy()
			end

			for _, tool in pairs(player.Backpack:GetChildren()) do
				if tool:FindFirstChild("Price") then
					tool:Destroy()
				end
			end

			for _, tool in pairs(character:GetChildren()) do
				if tool:FindFirstChild("Price") then
					tool:Destroy()
				end
			end

		end

		player:LoadCharacter()
	end

	status.Value = "Cleaning up last round"

	task.wait(3)

	newMap:Destroy()

	task.wait(7)
end

return round

These are just some things I found wrong with the script, sorry that it may not be the main problem.

1 Like

Unfortunately didn’t help, but I appreciate your help anyways.

Thanks!

Do you have any idea of where the script specifically breaks?

no, not at all. If I had to guess I’d say when it teleports every player. Sometimes it does teleport everyone, and sometimes it doesn’t. If it does, it skips over the round entirely

I’m really not sure what the problem is. Would you mind sending the place file here (or in private messages if you prefer)? It would help me to determine the problem more easily.

I could private message you one, I don’t really want random people downloading it :skull:

Sure, do whatever you’re comfortable with.

Alrighty, I sent the file…

The bug is really annoying and the game fr doesn’t tell me a thing :skull:

I appreciate you staying this patient. I would have lost all patience at this point

Lol, sorry btw for giving you a random solution notification.


I know it’s been nearly 3 months, but I was going through my old stuff and realized I never marked a solution for this post since you helped me in direct messages.

Sorry if it annoys you lol, just trying to organize some things.

1 Like