Help on error: "attempt to index nil with Event" on Bindable Event

So I started a tycoon tutorial on youtube but encountered an error where when I try to index a bindable event with event throws the error on the title. I tried to change the position of the bindable event variable inside the function where I use the “.Event” but that didn’t do.

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

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

Did you use Tycoon:SubscribeTopic to call the function?

I ran your module and it works for me. Can you throw off the part of the code where you create tycoon and use the function “SubscribeTopic” or all script

I ran the script too and I got an error.

Maybe it’s because of the mistake I made that I wrote Tycoon:SubscribeTopic instead of using the variable that I created.

I believe he made the same mistake as me.

local CollectionService = game:GetService("CollectionService")
local ServerStorage = game:GetService("ServerStorage")

local template = ServerStorage.Template
local componentFolder = script.Parent.Componente
local tycoonStorage = ServerStorage.TycoonStorage

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:Init()
	print(self.Owner)
	self.Model = NewModel(template, CFrame.new(0, 1, 0))
	self:LockAll()

	wait(7)
	self:PublishTopic("Button", "BasicPart")
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:Lock(instance)
	instance.Parent = tycoonStorage
	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: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, componentScript)
	local compModule = require(componentScript)
	local newComp = compModule.new(self, instance)
	newComp:Init()
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 

How did you manage to not get the error?

Send a script where you call SubscribeTopic

I think is this one, I have a server script from where I call Tycoon.new and then Tycoon:Init() but that’s all in the server script

local Unlockable = {}
Unlockable.__index = Unlockable

function Unlockable.new(tycoon, instance)
	local self = setmetatable({},Unlockable)
	self.Tycoon = tycoon
	self.Instance = instance
	
	return self
end

function Unlockable:Init()
	self.Subscribe = self.Tycoon:SubscribeTopic("Button", function(...)
		self:OnButtonPressed(...)
	end)
end

function Unlockable:OnButtonPressed(id)
	if id == self.Instance:GetAttribute("UnlockId") then
		
	end
end

return Unlockable

Try printing self when you’re creating a component (in Tycoon:CreateComponent)

I tried that too, In the Tycoon.new the bindable event appears but in the rest is just nil

Everything works for me, maybe you are not launching the modules themselves correctly or specifying the wrong parameters? Throw off the script that runs these modules

just to be sure, can i take a look in the server script? (perhaps you did TycoonModule:Init() instead of newTycoon:Init())

local Players = game:GetService("Players")

local Tycoon = require(script.Parent.Tycoon)

Players.PlayerAdded:Connect(function(player)
	local tycoon = Tycoon.new(player)
	Tycoon:Init()
end)

this is the culprit, you’re supposed to do tycoon:Init()

Ok now the error is gone but the part won’t unlock :crazy_face:

1 Like

you’d have to call :Unlock upon Unlockable:OnButtonPressed

1 Like

I will go now and take a break from my mental brake down. thank you king

1 Like

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