Attempt to index nil with "Name"

I am trying to make a part spawn on a random spawner by using OOP and Modules. Problem is though when I try to get the random spawners it keeps returning as nil. Even though I picked a random spawner. Here is the module

local SpawnModule = {}
--//This module handles random spanwer and gets spawners\\--
function SpawnModule.SetTable(table1)
	for index,value in pairs(game.Workspace.Spawns:GetChildren()) do
		table.insert(table1,value)
	end
end

function SpawnModule.ChoseRandomSpawn(table1)
	local Spawn = math.random(1,#table1)
	--local picked_value = table1[Spawn]
	--print(picked_value.Name)
end

return SpawnModule

MainScript

--local Object = require(script.Parent.Parent.Modules.ObjectModule)
local SpawnModule = require(script.Parent.Parent.Modules.SpawnModule)

local objects = {}

local part = game.ServerStorage:FindFirstChild("Part")
SpawnModule.SetTable(objects)
local spawner = SpawnModule.ChoseRandomSpawn(objects)
local picked_value = objects[spawner]
print(picked_value.Name)


--newobject = Object.new(randomSpawn.Position,part)

You never return or do anything with the commented out picked_value in the module.

3 Likes

Thank you for the help. Sorry it took me a while I was in school. So basiclly if you are going to use the object or value in another script you need to return it?

It’s not about being in another script, it’s just how functions work. You have your input and output. You give input by calling a function like functionName(input, goes, here) and your output by returning values from the function.

1 Like