TycoonModule:74: attempt to index nil with 'Event'

Hi, I was getting help from youtube to make a modern tycoon and I stumbled against a problem that wasn’t noticed in the video. I tried to fix it but nothing worked? Any idea why?

TycoonModule:74: attempt to index nil with 'Event'
--] Variables
local collectionService = game:GetService("CollectionService")
local assetFolder = game:GetService("ServerStorage").Assets
local storageFolder = game:GetService("ServerStorage").Storage
local componentFolder = game:GetService("ServerScriptService").Components
local template = assetFolder.Template
--] Functions
local function NewModel(model, cframe)
	local newModel = model:Clone()
	newModel:SetPrimaryPartCFrame(cframe)
	newModel.Parent = workspace
	return newModel
end

local Tycoon = {}
Tycoon.__index = Tycoon

function Tycoon.new(Player)
	local self = setmetatable({},Tycoon)
	self.Owner = Player
	self._topicEvent = Instance.new("BindableEvent")
	return self
end

function Tycoon:Initilize()
	self.Model = NewModel(template, CFrame.new(0, 1, 0))
	self:LockAll()

	wait(7)
	self:PublishTopic("Button", "BasicPart")
end

function Tycoon:Lock(instance)
	instance.Parent = storageFolder
	self:CreateComponent(instance,componentFolder.Unlockable)
end


function Tycoon:Unlock(instance, id)
	collectionService:RemoveTag(instance, "Unlockable")
	self:AddComponents(instance)
	instance.Parent = self.Model
end

function Tycoon:LockAll()
	for _,instance in ipairs(self.Model:GetDescendants()) do
		if collectionService:HasTag(instance,"Unlockable") then
			self:Lock(instance)
		else
			self:AddComponents(instance)
		end
	end
end


function Tycoon:AddComponents(instance)
	for _, tag in ipairs(collectionService:GetTags(instance)) do
		local component = componentFolder:FindFirstChild(tag)
		if component then 
			self:CreateComponent(instance, component)
		end
	end
end

function Tycoon:CreateComponent(instance,componentModule)
	local compModule = require(componentModule)
	local newComponent = compModule.new(self, instance)
	newComponent:Initilize()
end

function Tycoon:PublishTopic(topicName,...)
	self._topicEvent:Fire(topicName,...)
end

function Tycoon:SubscribeTopic(topicName,callback)
	local connection = self._topicEvent.Event:Connect(function(name,...)
		if name == topicName then
			callback(...)
		end
	end)
	return connection
end

function Tycoon:Destroy()
	self.Model:Destroy()
	self._topicEvent:Destroy()
end

return Tycoon

If you’re getting that error, you may want to check that self._topicEvent isn’t nil. Try printing it.

The error proofs it’s nil, but I’ll print it and here’s the output:

  22:16:58.949  Modern Tycoon auto-recovery file was created  -  Studio
  22:17:07.711  Event  -  Server - TycoonModule:22
  22:17:07.714  nil  -  Server - TycoonModule:77
  22:17:07.714  ServerScriptService.TycoonModule:78: attempt to index nil with 'Event'  - 
--] Variables
local collectionService = game:GetService("CollectionService")
local assetFolder = game:GetService("ServerStorage").Assets
local storageFolder = game:GetService("ServerStorage").Storage
local componentFolder = game:GetService("ServerScriptService").Components
local template = assetFolder.Template
--] Functions
local function NewModel(model, cframe)
	local newModel = model:Clone()
	newModel:SetPrimaryPartCFrame(cframe)
	newModel.Parent = workspace
	return newModel
end

local Tycoon = {}
Tycoon.__index = Tycoon

function Tycoon.new(Player)
	local self = setmetatable({},Tycoon)
	self.Owner = Player
	self._topicEvent = Instance.new("BindableEvent")
	print(self._topicEvent)
	return self
end

function Tycoon:Initilize()
	self.Model = NewModel(template, CFrame.new(0, 1, 0))
	self:LockAll()

	wait(7)
	self:PublishTopic("Button", "BasicPart")
end

function Tycoon:Lock(instance)
	instance.Parent = storageFolder
	self:CreateComponent(instance,componentFolder.Unlockable)
end


function Tycoon:Unlock(instance, id)
	collectionService:RemoveTag(instance, "Unlockable")
	self:AddComponents(instance)
	instance.Parent = self.Model
end

function Tycoon:LockAll()
	for _,instance in ipairs(self.Model:GetDescendants()) do
		if collectionService:HasTag(instance,"Unlockable") then
			self:Lock(instance)
		else
			self:AddComponents(instance)
		end
	end
end


function Tycoon:AddComponents(instance)
	for _, tag in ipairs(collectionService:GetTags(instance)) do
		local component = componentFolder:FindFirstChild(tag)
		if component then 
			self:CreateComponent(instance, component)
		end
	end
end

function Tycoon:CreateComponent(instance,componentModule)
	local compModule = require(componentModule)
	local newComponent = compModule.new(self, instance)
	newComponent:Initilize()
end

function Tycoon:PublishTopic(topicName,...)
	self._topicEvent:Fire(topicName,...)
end

function Tycoon:SubscribeTopic(topicName,callback)
	print(self._topicEvent)
	local connection = self._topicEvent.Event:Connect(function(name,...)
		if name == topicName then
			callback(...)
		end
	end)
	return connection
end

function Tycoon:Destroy()
	self.Model:Destroy()
	self._topicEvent:Destroy()
end

return Tycoon

Okay. I see you’re using oop, Is _topicEvent inherited?

No I don’t think so. _topicEvent prints out as “Event” on Tycoon.new() but prints out nil on line 77 (where the error occurs)

_topicEvent is first defined in TycoonModule so no it isn’t inherited

Hm. Is there any point in your code where you’re reassigning it? also, print self to make sure it’s what it should be also.

Self prints out a table with all the stuff that is expected to be there except the event

I’m really confused as well I think I did everything correctly

Did you make sure to reload the module after making changes?

local t = require(game.ReplicatedStorage.t) local y = t.new("a") print(y._topicEvent.Event)  -  Studio
  04:58:37.019  Signal Event  -  Edit

It works for me

I don’t know what you mean about reload but I tried the same thing on the script that initializes

--] Modules
local TycoonModule = require(game:GetService("ServerScriptService").TycoonModule)
--] Services
local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(Player)
	local Tycoon = TycoonModule.new(Player)
	TycoonModule:Initilize() -- Initilizing the module
end)

local t = TycoonModule.new()

print(t._topicEvent)

It worked but the problem still remains inside the module

just a small logic error

here you must call the method through the instance, not the class.

Tycoon:Initilize() -- Initilizing the module
1 Like

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