ServerScriptService.Main:6: attempt to index nil with 'Clone'

So im maded a map vote system and i got problem when the voting ends and map was chosen i think because its not returning winner name map but i dont know how to fix this
here is my 1 script(voting):

local module = {}

local status = game.ReplicatedStorage.Values.Status

local mapVotes = {}


function getMap()
	local randomMap = game.ReplicatedStorage.Maps:GetChildren()[math.random(1,#game.ReplicatedStorage.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
	local event = game.ReplicatedStorage.Events.RemoteEvent
	event.OnServerEvent:Connect(function()
		mapVotes = {}

		for i = 1,3 do
			table.insert(mapVotes, {order = i; name = getMap(); players = {};})
		end

		game.ReplicatedStorage.Events.UpdateVotingSystem:FireAllClients(mapVotes)
		game.ReplicatedStorage.Values.VotingProgress.Value = true
	

		local placeVoteConnection = game.ReplicatedStorage.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.Events.UpdateVotingSystem:FireAllClients(mapVotes)
		
		end)

		for i = dur,0,-1 do
			if game.ReplicatedStorage.Values.VotingProgress.Value == true then
				status.Value = "You have ".. i .." "..((i==1 and "second") or "seconds").." to vote!"
				task.wait(1)
			end
		end
		
	
		if game.ReplicatedStorage.Values.Status.Value == "You have 0 seconds to vote!" then
			-- End the voting system
			placeVoteConnection:Disconnect()

			game.ReplicatedStorage.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!"
			game.ReplicatedStorage.Values.value2.Value = 1		
			return game.ReplicatedStorage.Maps:FindFirstChild(winner.name)
		end
	end)
end
return module

here is my 2 script:

local voting = require(script.Voting)

while true do
	local chosenMap = voting.MapVoting(20)
	if game.ReplicatedStorage.Values.value2.Value == 1 then
		local clonedMap = chosenMap:Clone()

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

	wait(1)
end

voting.MapVoting must not be returning a value. You should use breakpoints to find out why it’s not.

I’m not great at module strips but shouldn’t you call the exact function?

local chosenMap = voting.module.MapVoting(20)

Nope not working got this error:
ServerScriptService.Main:4: attempt to index nil with ‘MapVoting’

Is the module script is inside the second script inside ServerScriptService?

Yes it is inside the second script inside ServerScriptService

then right before you return print what you want to return .

print(game.ReplicatedStorage.Maps:FindFirstChild(winner.name))
	return game.ReplicatedStorage.Maps:FindFirstChild(winner.name)

Can you show how your explorer hierarchy. As :FindFirstChild() returns nil if it fails to find the Instance it’s asked for.

so that is what you’r sending. what if remove the name

print(game.ReplicatedStorage.Maps:FindFirstChild(winner))

1 Like

image

1 Like

is it a timing thing ? try this.

local voting = require(script.Voting)

while true do
	if voting.MapVoting() == nil then
	else
		local chosenMap = voting.MapVoting(20)

		if game.ReplicatedStorage.Values.value2.Value == 1 then
			local clonedMap = chosenMap:Clone()

			clonedMap.Name = "Map"
			clonedMap.Parent = workspace
		end
	end
	wait(1)
end


got this error

So the dur was nil. Do you use the function from anywhere else?

nope i didnt used this function anywhere else

Could you check if status is getting set correctly? If not, most likely the code above that determines the winner is not working as intended.

yes its working correctly and fine

if you set dur to 20 and do not pass it does the script error?

What, you’re doing return (winner.name). First of all, the :Clone() method takes an Instance, not a String (ignore this im wrong). Additionally, name is seen as a member of the value being indexed, because it has to be Name.

return game.ReplicatedStorage.Maps:FindFirstChild(winner)

Additionally, you can’t do .Name because in your code, winner is not an Instance, which does not have said property unless you assigned it.