Script timeout: exhausted allowed execution time

I have created a round system for my Roblox game, but usually in the 4th round in the testing place and the 3rd round in the main place, an error occurs:

Script timeout: exhausted allowed execution time - Server - Gifts:52
Screenshot 2023-12-10 195358

The mentioned error appears both in the testing place and also in the main place. Then, another error occurs but only in the main place.

ServerScriptService.RoundSystem:213: Script timeout: exhausted allowed execution time - Server - RoundSystem:87
Screenshot 2023-12-10 195402

Gifts Script:

local predefinedColors = {
	Color3.new(0, 1, 0),    -- Verde
	Color3.new(0, 0, 1),    -- Blu
	Color3.new(1, 1, 0),    -- Giallo
	Color3.new(1, 0, 1),    -- Magenta
	Color3.new(0.0431373, 0.615686, 1),    -- Ciano
	Color3.new(1, 0.478431, 0.0196078),  -- Marrone
	Color3.new(0, 0.5, 0),  -- Verde scuro
	Color3.new(0.321569, 0.054902, 1),  -- Blu scuro
	Color3.new(1, 1, 1)     -- Bianco
}

-- Definisci il modello da clonare
local modelToClone = script.Gift -- Sostituisci "ModelToClone" con il nome del tuo modello da clonare

-- Ottieni la cartella "GiftPositions" in workspace
local giftPositionsFolder = game.Workspace.Map1GiftsPosition

-- Carica il servizio ReplicatedStorage
local ReplicatedStorage = game:GetService("ReplicatedStorage")

-- Ottieni l'oggetto InRound da ReplicatedStorage
local inRoundValue = ReplicatedStorage:WaitForChild("GiftOutput")
local newValue = ReplicatedStorage:WaitForChild("GiftOutput")

-- Funzione per clonare e posizionare il modello
local function cloneAndPlaceModel(position, orientation, color, i)
	local clone = modelToClone:Clone()
	clone.Parent = game.Workspace.Gifts
	clone.Name = i
	local Highlight = Instance.new("Highlight")
	Highlight.Parent = clone
	Highlight.FillTransparency = 1
	Highlight.OutlineTransparency = 0
	Highlight.DepthMode = Enum.HighlightDepthMode.Occluded
	Highlight.Adornee = clone
	
	--clone.Highlight.Adornee = clone

	clone.GiftColor.BrickColor = BrickColor.new(color)

	-- Posiziona il clone nella posizione specificata
	clone:SetPrimaryPartCFrame(CFrame.new(position, position + orientation))
end

-- Lista di posizioni uniche per i regali
local uniquePositions = {}

local giftmax = game.ReplicatedStorage.GiftLeft.Value

-- Funzione per eseguire il cloning e il posizionamento quando InRound diventa "GiftSpawning"
inRoundValue.Changed:Connect(function()
	if newValue.Value == "SpawnGiftsPls" then
		newValue.Value = "SpawningGifts"
		-- Clona e posiziona il modello 50 volte
		for i = 1, giftmax do
			local randomIndex
			repeat
				randomIndex = math.random(1, #giftPositionsFolder:GetChildren())
			until not uniquePositions[randomIndex]

			uniquePositions[randomIndex] = true
			local giftPosition = giftPositionsFolder:GetChildren()[randomIndex].Position
			local giftOrientation = giftPositionsFolder:GetChildren()[randomIndex].CFrame.LookVector

			local randomColorIndex = math.random(1, #predefinedColors)
			local randomColor = predefinedColors[randomColorIndex]

			cloneAndPlaceModel(giftPosition, giftOrientation, randomColor, i)
		end
		newValue.Value = "GiftsSpawned"
		print("Clonati e posizionati tutti i regali.")
	elseif newValue.Value == "PlsRemoveGifs" then
		for i,v in ipairs(game.Workspace.Gifts:GetChildren()) do
			v:Destroy()
		end
	end
end)

Round Script:

-- Dichiarazione delle costanti
local ROUND_TIME = 10
local requiredPlayers = 1 -- Imposta il numero di giocatori richiesto
local MAX_GIFTS = 50 -- Imposta il numero massimo di regali

-- Variabili di gioco
local intermissionCountdown = 10 -- Durata dell'intermission countdown
local roundCountdown = 3 -- Durata del countdown prima dell'inizio del round
local roundended = true
local VIPGamepassId = 662232158

local GiftOutput = game.ReplicatedStorage.GiftOutput

function secondsToMinutesSeconds(seconds)
	local minutes = math.floor(seconds / 60)
	local remainingSeconds = seconds % 60
	return string.format("%02d:%02d", minutes, remainingSeconds)
end

-- Funzione per resettare le impostazioni della mappa
local function resetMapSettings()
	game.ReplicatedStorage.GiftLeft.Value = MAX_GIFTS
	for h,i in ipairs( game.Workspace.Maps:GetChildren()) do
		local barriersFolder = i.Barriers:GetChildren()
		for _, barrier in ipairs(barriersFolder) do
			barrier.CanCollide = true
		end
	end

	game.ReplicatedStorage.Start:FireAllClients()
end

-- Funzione per teletrasportare i giocatori
local function teleportPlayers(selectedMap)

	local spawnLocations = selectedMap.Spawns:GetChildren()

	for _, player in pairs(game.Teams.Lobby:GetPlayers()) do
		local randomIndex = math.random(1, #spawnLocations)
		local spawnPosition = spawnLocations[randomIndex].Position
		if player.Character then
			player.Character:SetPrimaryPartCFrame(CFrame.new(spawnPosition) + Vector3.new(0, 5, 0))
		end
		player.Team = game.Teams.Playing
	end
end

-- Funzione per aggiornare lo status lato client
local function updateStatusToClient(status)
	game.ReplicatedStorage.Status.Value = status
end

-- Funzione per gestire l'intermission
local function startIntermission()
	resetMapSettings()

	for countdown = intermissionCountdown, 0, -1 do
		if #game.Teams.Lobby:GetPlayers() >= requiredPlayers then
			game.ReplicatedStorage.TextStatus.Value = "Game starting in " .. countdown
			wait(1)
		else
			game.ReplicatedStorage.TextStatus.Value = "Waiting for players..."
			updateStatusToClient("WaitingForRequiredPlayers")

			for _ = 1, intermissionCountdown do
				wait(1)
				if roundended or #game.Teams.Lobby:GetPlayers() >= requiredPlayers then
					break
				end
			end

			if #game.Teams.Lobby:GetPlayers() < requiredPlayers then
				countdown = intermissionCountdown  -- Ripartire da 10 quando ci sono abbastanza giocatori
			end
		end
	end
	
	local Maps = game.Workspace.Maps:GetChildren()
	local randommap = math.random(1, #Maps)
	local selectedMap = Maps[randommap]
	game.ReplicatedStorage.TextStatus.Value = "The round map will be "..selectedMap.Name
	teleportPlayers(selectedMap)
	
	GiftOutput.Value = "SpawnGiftsPls"
	
	-- Fire Testing2 event to clients
	game.ReplicatedStorage.Testing2:FireAllClients()
	
	for i = roundCountdown, 0, -1 do
		game.ReplicatedStorage.TextStatus.Value = "The round is starting..."
		wait(1)
	end

	-- Update barrier transparency
	game.ReplicatedStorage.uwu.OnServerEvent:Connect(function()
		local barriersFolder = selectedMap.Barriers:GetChildren()
		for _, s in ipairs(barriersFolder) do
			s.CanCollide = false
		end
	end)
	-- Inizio del round
	updateStatusToClient("RoundInProgress")
	
	for i = ROUND_TIME, 0, -1 do
		game.ReplicatedStorage.TextStatus.Value = secondsToMinutesSeconds(i)
		wait(1)
	end

	-- Fine del round
	updateStatusToClient("RoundEnded")
	GiftOutput.Value = "PlsRemoveGifs"
	game.ReplicatedStorage.RoundEndedResult:FireAllClients()
	game.ReplicatedStorage.TextStatus.Value = "Round Ended"

	-- Distribuzione dei premi
	local sortedPlayers = {}
	for _, player in pairs(game.Teams.Playing:GetPlayers()) do
		table.insert(sortedPlayers, player)
	end

	table.sort(sortedPlayers, function(a, b)
		return a.Gifts.Value > b.Gifts.Value
	end)

	-- Distribuzione dei premi
	local numberOfPlayers = #sortedPlayers

	if numberOfPlayers >= 1 then
		local winner = sortedPlayers[1]
		winner.leaderstats.Wins.Value += 1
		if winner.Gifts.Value >= 1 then
			if (game:GetService("MarketplaceService"):UserOwnsGamePassAsync(winner.UserId, VIPGamepassId)) then -- 3x gamepass
				winner.Coins.Value += 200
			else
				winner.Coins.Value += 100
			end
		end
		game.ReplicatedStorage.TextStatus.Value = sortedPlayers[1].Name .. " has won the round with " .. sortedPlayers[1].Gifts.Value .. " gifts!"
		local plr1 = game.Players:FindFirstChild(sortedPlayers[1].Name)
		if plr1.Character then
			plr1.Character:SetPrimaryPartCFrame(CFrame.new(game.Workspace.EndingArea.First.Position) + Vector3.new(0, 5, 0))
		end
	end

	if numberOfPlayers >= 2 then
		local winnerB = sortedPlayers[1]
		winnerB.leaderstats.Wins.Value += 1
		if winnerB.Gifts.Value >= 1 then
			if (game:GetService("MarketplaceService"):UserOwnsGamePassAsync(winnerB.UserId, VIPGamepassId)) then -- 3x gamepass
				winnerB.Coins.Value += 200
			else
				winnerB.Coins.Value += 100
			end
		end
		local plr2 = game.Players:FindFirstChild(sortedPlayers[2].Name)
		if plr2.Character then
			plr2.Character:SetPrimaryPartCFrame(CFrame.new(game.Workspace.EndingArea.Second.Position) + Vector3.new(0, 5, 0))
		end
	end

	if numberOfPlayers >= 3 then
		local winnerC = sortedPlayers[1]
		winnerC.leaderstats.Wins.Value += 1
		if winnerC.Gifts.Value >= 1 then
			if (game:GetService("MarketplaceService"):UserOwnsGamePassAsync(winnerC.UserId, VIPGamepassId)) then -- 3x gamepass
				winnerC.Coins.Value += 200
			else
				winnerC.Coins.Value += 100
			end
		end
		local plr3 = game.Players:FindFirstChild(sortedPlayers[3].Name)
		if plr3.Character then
			plr3.Character:SetPrimaryPartCFrame(CFrame.new(game.Workspace.EndingArea.Therd.Position) + Vector3.new(0, 5, 0))
		end
	end

	for i = 4, numberOfPlayers do
		local winnerD = sortedPlayers[i]
		winnerD.leaderstats.Wins.Value += 1
		if (game:GetService("MarketplaceService"):UserOwnsGamePassAsync(winnerD.UserId, VIPGamepassId)) then -- 3x gamepass
			winnerD.Coins.Value += 20
		else
			winnerD.Coins.Value += 10
		end
	end
	
	game.ReplicatedStorage.CameraA:FireAllClients()
	
	wait(10)
	
	game.ReplicatedStorage.CameraB:FireAllClients()
	

	-- Teletrasporto dei giocatori nella lobby per l'intermission successiva
	for _, player in pairs(game.Teams.Playing:GetPlayers()) do
		local spawnLocations = game.Workspace.LobbyMap.Spawns:GetChildren()
		local randomIndex = math.random(1, #spawnLocations)
		local spawnPosition = spawnLocations[randomIndex].Position
		player.Gifts.Value = 0
		if player.Character then
			player.Character:SetPrimaryPartCFrame(CFrame.new(spawnPosition) + Vector3.new(0, 5, 0))
		end
		player.Team = game.Teams.Lobby
	end

	roundended = true
end

-- Chiamata della funzione per avviare l'intermission
repeat
	if roundended and #game.Teams.Lobby:GetPlayers() >= requiredPlayers then
		roundended = false
		startIntermission()
	else
		updateStatusToClient("WaitingForRequiredPlayers")
		game.ReplicatedStorage.TextStatus.Value = "At least " .. requiredPlayers .. " players are needed to start a round."

		repeat
			wait(1)
		until #game.Teams.Lobby:GetPlayers() >= requiredPlayers or roundended
	end

	-- Attendere che il round abbia completato prima di eseguire nuovamente il ciclo
	repeat
		wait(1)
	until roundended
until false -- Puoi inserire la tua condizione di uscita qui

Testing Place File:
TestingPlace.rbxl (118.0 KB)

1 Like

check your repeat and while loops

while true do
  wait()
end

repeat
  wait() -- "Don't forget to yield otherwise it'll loop as fast as possible"
until
1 Like

Thanks for your response, but I managed to fix it on my own. It seems I forgot to reset the local library uniquePositions = {}. Only that missing line of code was causing the error in two scripts.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.