Stack Overflow error

The issue is that i keep getting this error
Stack Overflow

then the game freezes for abit.

Code

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Remotes = ReplicatedStorage.Remotes
local Maps = ReplicatedStorage:WaitForChild("Maps")
local RunService = game:GetService("RunService")
local GameValues = ReplicatedStorage:WaitForChild("GameValues")
local VotingingInProgress = GameValues:WaitForChild("VotingProgress")
local VotingHandler = {}
VotingHandler.mapVotes = {}


ReplicatedStorage.Remotes.Voting.PlaceVote.OnServerEvent:Connect(function(player,padNumb)
	for i,v in VotingHandler.mapVotes do
		for x,plr in v.players do
			if plr == player.UserId then
				table.remove(v.Players,x)
				break
			end
		end
	end
	for i,v in VotingHandler.mapVotes do
		if v.order == padNumb then
			table.insert(v.players,player.UserId)
		end
	end
end)

function getMap()
	local randMap = Maps:GetChildren()[math.random(1,#Maps:GetChildren())]
	for i,v in VotingHandler.mapVotes do
		if v.name == randMap.name then
			return getMap()
		end
	end
	return randMap.Name
end

function VotingHandler.MapVoting(dur)
	VotingHandler.mapVotes = {}

	for i,v in workspace.World.VotingBoard.VotingPads:GetChildren() do
		table.insert(VotingHandler.mapVotes, {order = tonumber(v.Name:match("%d+")); name = getMap(); Players = {}})
		
		print(v.Name)
	end
	GameValues.VotingProgress.Value = true
	
	local start = tick()	
	
	while task.wait() do
		if tick() - start >= dur then break end
		
		GameValues.Status.Value = "[Voting: " .. math.floor(dur - (tick() - start)) .. "seconds left to vote]"
		for i,v in workspace.World.VotingBoard.VotingPads:GetChildren()  do
			local votes,mapName
			for x,map in VotingHandler.mapVotes do
				if map.order == tonumber(v.Name:match("%d+")) then
					votes = #map.Players
					mapName = map.name
					
					break
				end
			end
			
			GameValues.VotingProgress = false
			
			table.sort(VotingHandler.mapVotes,function(a,b) return #a.Players > #b.Players end)
			
			GameValues.Status.Value = "[ Loading map: " .. VotingHandler.mapVotes[1].name .. "! ]"
			
			task.wait(3)
			return Maps:FindFirstChild(VotingHandler.mapVotes[1].name)
		end
	end
	
end

return VotingHandler
3 Likes

I have only skim read the code, so I might be wrong but I’m pretty sure this looks like it would be called recursively to give you the error.

Stack overflow

The call stack is how the program keeps track of where it should return to after it finishes the called function, but to do so it uses memory, so to prevent a recursive function from just crashing the whole computer by using all the free memory there is usually an upper limit, breaching this is the stack overflow. Most normal programming wouldn’t get close to the limit, but where a function repetitively calls itself it can do this very quickly!

I’m confused in what you hope to do here with the recursive function call?

4 Likes

Could it be an issue with the getMap function then? I’m guessing since I still think this would give a script timeout error, but getMap is being called recursively over and over again extremely fast and presumably exponentially since it looks like OP forgot to capitalize Name in the condition for getMap running. Unless that’s a different property, I suspect that condition is always returning nil == nil which is true.

Assuming just two items are in that table, that means the amount of functions running at once doubles every iteration.

1 Like

im aware that it should be “Name” but for some reason it was giving the option of name not Name

1 Like

ok i will change that for now to see if it works

1 Like

ok so it works but i do think it wont select a random map if no one votes but i will do that my self.

1 Like

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