Do u know how I can fix this?

I would like to know how I can improve my script so it doesn’t timeout whenever a player is teleported to the chosen map.

local voting = require(script.Voting)
local plr = game.Players
local playing = {}
local status = game.ReplicatedStorage:WaitForChild("Voting").Values.Status
local text = game.StarterGui:WaitForChild("MainGui").Status

local remote = game.ReplicatedStorage:WaitForChild("Disasters").Remotes.RemoteEvent


local Commondisasters = {
	"Meteor Shower",
	"Accid Rain",
	"EarthQuake",
	"Tornado",
	"Sand Storm",
	"Lighting Storm"
}

local AdminDisasters = {
	"Zombye Apocalypse",
	"Alien Invasion",
	"Nuclear Bomb"
}

while true do
	
	wait()
	local chosenMap = voting.MapVoting(10)
	local clonedMap = chosenMap:Clone()
	local TeleportPoint = clonedMap:WaitForChild("TeleportPoint").CFrame

	clonedMap.Name = "Map"
	clonedMap.Parent = workspace

	
	
	--Getting the players
	for i, v in pairs(game.Players:GetChildren()) do
		local char = v.Character
		if char then
			local hrp = char:WaitForChild("HumanoidRootPart")
			
			hrp.CFrame = clonedMap.TeleportPoint.CFrame
			table.insert(playing,v.Name)
			
			char:WaitForChild("Humanoid").Died:Connect(function()
				table.remove(playing,v.Name)
			end)			
		end	
		
		
		if #playing <=2 then
			task.wait()
			
		elseif #playing == 1 then
			status.Value = table.find(playing, v.Name)
		end]]
		local randomNumber = math.random(5,10)
		task.wait(randomNumber)

		--Disaster
		wait()
		local chosenDisaster = math.random(1,100)
		if chosenDisaster <= 90 then
			chosenDisaster = math.random(1,#Commondisasters)
			remote:FireAllClients(Commondisasters[chosenDisaster])
			
		elseif chosenDisaster >= 10 then
			chosenDisaster = math.random(1,#AdminDisasters)
			remote:FireAllClients(AdminDisasters[chosenDisaster])
		end
		remote:FireAllClients(AdminDisasters[chosenDisaster])
		status.Value = "Bum Bum, coco is watching:)"
		v.PlayerGui.MainGui.Status.TextColor3 = Color3.fromRGB(255, 31, 15)
		
	end
end

Anything is apreciated, so if u have any ideas let me know.

1 Like

i’m not 100% sure about this, but I think scripts only time-out when it’s running in studio, so it shouldn’t happen in-game. (I’m assuming that you’re talking about this: “Script timeout: exhausted allowed execution time”)
Also can you send an image of the error so I can see exactly where it times out?


Sure. Here u go

Is the function voting.MapVoting(10) that is called at the top of the while loop computationally intensive?

Also, is the name of the script that the code is located in named “Store”?

2 Likes
local module = {}

local status = game.ReplicatedStorage.Voting.Values.Status

local mapVotes = {}

--[[
local mapVotes = {
	{order = 1;name = "Grass";players = {}},
	{order = 2;name = "Snow";players = {}},
	{order = 3;name = "Desert";players = {}}
}
]]

function getMap()
	local randomMap = game.ReplicatedStorage:WaitForChild("Voting").Maps:GetChildren()[math.random(1,#game.ReplicatedStorage.Voting.Maps:GetChildren())]
	
	-- if map exists in the mapVotes table
	for i,map in pairs(mapVotes) do
		if map.name == randomMap.Name then
			return getMap()
		end
	end
	
	return randomMap.Name
end

function module.MapVoting(dur)
	-- Start the voting system
	mapVotes = {}
	
	for i = 1,3 do
		table.insert(mapVotes, {order = i; name = getMap(); players = {};})
	end
	
	game.ReplicatedStorage.Voting.Events.UpdateVotingSystem:FireAllClients(mapVotes)
	
	game.ReplicatedStorage.Voting.Values.VotingProgress.Value = true
	
	local placeVoteConnection = game.ReplicatedStorage.Voting.Events.PlaceVote.OnServerEvent:Connect(function(player,voteNumber)
		
		-- Check if already voted
		for i,map in pairs(mapVotes) do
			for x,plr in pairs(map.players) do
				if plr == player.UserId then
					table.remove(map.players, x)
					break
				end
			end
		end
		
		-- Place Vote
		for i,map in pairs(mapVotes) do
			if map.order == voteNumber then
				table.insert(map.players, player.UserId)
			end
		end
		
		game.ReplicatedStorage.Voting.Events.UpdateVotingSystem:FireAllClients(mapVotes)
	end)
	
	for i = dur,0,-1 do
		status.Value = "You have ".. i .." "..((i==1 and "second") or "seconds").." to vote!"
		task.wait(1)
	end
	
	-- End the voting system
	placeVoteConnection:Disconnect()
	
	game.ReplicatedStorage.Voting.Values.VotingProgress.Value = false
	
	-- Get the winner and reveal
	table.sort(mapVotes, function(a,b) return #a.players > #b.players end)
	
	local winner
	
	-- If the all pads have the same vote
	if #mapVotes[1].players == #mapVotes[2].players and #mapVotes[2].players == #mapVotes[3].players then
		winner = mapVotes[math.random(1,3)]
		-- If 2 pads have the same vote
	elseif #mapVotes[1].players == #mapVotes[2].players then
		winner = mapVotes[math.random(1,2)]
	else
		winner = mapVotes[1]
	end
	
	status.Value = winner.name.." was chosen!"
	
	
	return game.ReplicatedStorage.Voting.Maps:FindFirstChild(winner.name)
end


return module

this is the module script and it is located in the main script. The module is named Voting