Map Voting System Bug

Hey! Im trying to create a Map Voting system based in the workspace like Murder Mystery 2!

Whenever I choose a Random Map (in a function) it continuously returns Nil…
image

I’ve tried looking on the hub, and I’ve tried basically everything in my knowledge scripting wise

Im not even sure why this can be happening to be honest…

(function that chooses them)

local function randomMap()
	local random = math.random(1,#mapsFolder:GetChildren())

	local chosen = mapsFolder:GetChildren()[random].Name

	if not table.find(selectedMaps, chosen) then
		table.insert(selectedMaps,chosen)
		print(chosen)
		return chosen
	else
		randomMap()
	end
end

(choose the random maps)

local map1 = mapsFolder:FindFirstChild(randomMap())
local map2 = mapsFolder:FindFirstChild(randomMap())
local map3 = mapsFolder:FindFirstChild(randomMap())

(line that is corrupt)

local map2 = mapsFolder:FindFirstChild(randomMap())

Try this:

local function randomMap()
	local AllMaps = mapsFolder:GetChildren()
	local rnd = math.random(1,#AllMaps)

	local chosen = AllMaps[rnd]

	if not table.find(selectedMaps, chosen.Name) then
		table.insert(selectedMaps,chosen.Name)
		print(chosen.Name)
		return chosen
	else
		return randomMap()
	end
end


local map1 = randomMap()
local map2 = randomMap()
local map3 = randomMap()

– EDIT

Just to clarify, your script was ok too, the only issue you has was not returning the result in this part:

else
	randomMap()
end

which is why it’s returning nil for the second map

1 Like

thank you so much haha. For now this works, but the error would come up at random times as if one of the maps were spelled incorrectly (which they werent lol). Ill get back to you if any errors pop up! :smiley:

1 Like