Why does this error?

  1. What do you want to achieve? Make a random map choosing script that has devproducts to choose the map and randomness.
  2. What is the issue? Error: [09:05:57.233 - Workspace.map.main:80: attempt to index nil with ‘Name’]
  3. What solutions have you tried so far? Pcalled it.

Script:

local f = require(script.functions)
local wp = workspace
local runService = game:GetService("RunService")
local map = wp.map
local serverStorage = game:GetService("ServerStorage")
local replicatedStorage = game:GetService("ReplicatedStorage")
local mapStorage = serverStorage.maps
local players = game:GetService("Players")
local marketplaceService = game:GetService("MarketplaceService")
local mapQueue = {}
local productIds = {
	chooseMap = 1008767645
}

local chosenMap
local mapTokens = wp.data.mapTokens
local playersLeft = wp.data.playersLeft.Value
local gameRunning = wp.data.gameRunning.Value
local gamesPlayedTotal = wp.data.gamesPlayedTotal.Value

players.PlayerAdded:Connect(function(plr)
	local mapTokVal = Instance.new("IntValue",mapTokens)
	mapTokVal.Name = plr.Name
end)


local function processReceipt(receiptInfo)
	local plr = players:GetPlayerByUserId(receiptInfo.PlayerId)
	if not plr then
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end
	
	if plr then
		if receiptInfo.ProductId == productIds.chooseMap then
			local name = players:GetPlayerByUserId(receiptInfo.PlayerId).Name
			local plr = players:GetPlayerByUserId(receiptInfo.PlayerId)
			mapTokens:FindFirstChild(name).Value = mapTokens:FindFirstChild(name).Value + 1
			replicatedStorage.remote.closeShopOpenChooseMap:FireClient(plr)

			return Enum.ProductPurchaseDecision.PurchaseGranted
		end
	end
end
marketplaceService.ProcessReceipt = processReceipt 

replicatedStorage.remote.selectMap.OnServerEvent:Connect(function(plr,map)
	if mapTokens:FindFirstChild(plr.Name).Value > 0 then
		mapTokens:FindFirstChild(plr.Name).Value = mapTokens:FindFirstChild(plr.Name).Value - 1
		table.insert(mapQueue,map)
	end
end)

local function setStatus(status)
	replicatedStorage.remote.setStatus:FireAllClients(status)
end

while wait() do
	mapTokens = wp.data.mapTokens
	playersLeft = wp.data.playersLeft.Value
	gameRunning = wp.data.gameRunning.Value
	gamesPlayedTotal = wp.data.gamesPlayedTotal.Value
	
	if gameRunning == false then	
		if gamesPlayedTotal > 0 then setStatus("Game ended!") end
		wait(1)
		setStatus("Choosing map...")
		wait(1)
		for i,v in pairs(map:GetChildren()) do
			if v:IsA("Model") then
				v:Destroy()
			end
		end	
		wait(0.5)
		local builtMap,usedQueue = f.buildMap(map,mapStorage,mapQueue)
		if usedQueue == true then
			table.remove(mapQueue,1)
			print("usedtable")
		end
		
		setStatus("The map "..builtMap.Name.." was chosen!")
		wait(1)
	end
end

Module:

local m = {}
function m.buildMap(map,maps,mapQueue)
	if mapQueue[1] == nil then
		local cCFrame = CFrame.new(0,0,0)
		
		local modelList = maps:GetChildren()
		local rand = Random.new()
		local model = modelList[rand:NextInteger(1,#modelList)]
				
		local modelClone = model:Clone()
		modelClone:SetPrimaryPartCFrame(cCFrame)
		modelClone.Parent = map
		return modelClone,false
	elseif mapQueue[1] ~= nil then
		local pcallClone
		local suc,err = pcall(function()
			local cCframe = CFrame.new(0,0,0)
			
			local modelFolder = maps
			local model = modelFolder:FindFirstChild(mapQueue[1])
			
			if model then
				local modelClone = model:Clone()
				modelClone.SetPromaryPartCFrame(cCframe)
				modelClone.Parent = map
				pcallClone = modelClone
			end
		end)
		if not suc then
			local cCFrame = CFrame.new(0,0,0)
			
			local modelList = maps:GetChildren()
			local rand = Random.new()
			local model = modelList[rand:NextInteger(1,#modelList)]
					
			local modelClone = model:Clone()
			modelClone:SetPrimaryPartCFrame(cCFrame)
			modelClone.Parent = map
			return modelClone,true
		elseif suc then
			return pcallClone,true
		end
	end 
end
return m

Also im trying to make it error to test if something changes the value of the name of the map in the playergui then it doesnt error but it uses random picking of the map.

1 Like

why did it error?
because on line 80 you attempted to index nil with ‘Name’

line 80

setStatus("The map "..builtMap.Name.." was chosen!")


why was builtMap nil?
because f.buildMap returned nil, true

why did it return a nil map?
because your pcall was still technically successful even if it didn’t define pcallClone

your code:

local suc,err = pcall(function()
	local cCframe = CFrame.new(0,0,0)
	
	local modelFolder = maps
	local model = modelFolder:FindFirstChild(mapQueue[1])
	
	if model then
		local modelClone = model:Clone()
		modelClone.SetPromaryPartCFrame(cCframe)
		modelClone.Parent = map
		pcallClone = modelClone
	end
end)

suc was true even though the if model then statement never ran, so pcallClone wasn’t defined

then you returned pcallClone, which was not defined

elseif suc then
	return pcallClone,true



suc will be true if your pcalled function did not error

--example of pcall behavior you didn't expect
local suc = pcall(function()
     if 2 == 3 then
          print("nice")
     end
end)

print(suc) -> true


​​

how do you fix it?
that depends on how much time and effort you want to spend on fixing it

personally your Module code doesn’t make sense to me and I’d rewrite it

but you could fix this one problem (though there may be more waiting for you) by simply removing the if statement from your pcall so it looks like this instead

local suc,err = pcall(function()
	local cCframe = CFrame.new(0,0,0)
	
	local modelFolder = maps
	local model = modelFolder:FindFirstChild(mapQueue[1])

	local modelClone = model:Clone()
	modelClone.SetPromaryPartCFrame(cCframe)
	modelClone.Parent = map
	pcallClone = modelClone
end)


If you do that, I suspect it will run just about how you expected it to run when you wrote it

If you feel comfortable, you can also do it without the unnecessary pcall that made a few things worse

Thanks. Again message character limit :stuck_out_tongue:

1 Like