You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
So basically i’m making a tycoon and using a module script with self thing defined. I have self.Model defined as template and when i’m trying to get descendants in other function i get an error "attempt to index nil with GetDescendants
What is the issue? Include screenshots / videos if possible!
What solutions have you tried so far? Did you look for solutions on the Developer Hub? I was making it by youtube video, and it should work, but it’s don’t.
Code:
local CollectionService = game:GetService("CollectionService")
local template = game:GetService("ServerStorage").Template
local componentFolder = script.Parent.Components
local tycoonStorage = game:GetService("ServerStorage").TycoonStorage
local function NewModel(model, cframe)
local newModel = model:Clone()
newModel:PivotTo(cframe)
newModel.Parent = workspace
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()
self.Model = NewModel(template, CFrame.new(-238, 36.25, -165))
self:LockAll()
end
function Tycoon:LockAll()
for _, instance in ipairs(self.Model:GetDescendants()) do -- Here is error
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: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
Well now code which should work don’t work :/, it worked before.But i don’t know why it don’t work now lol
local CollectionService = game:GetService("CollectionService")
local template = game:GetService("ServerStorage").Template
local componentFolder = script.Parent.Components
local tycoonStorage = game:GetService("ServerStorage").TycoonStorage
local function NewModel(model, cframe)
local newModel = model:Clone()
newModel:PivotTo(cframe)
newModel.Parent = workspace
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()
self.Model = NewModel(template, CFrame.new(-238, 36.25, -165))
self:LockAll()
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: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() -- new error here
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
So basically now i get error while trying to get while Bindable Event is fired.
Here is getting code:
self._topicEvent = Instance.new("BindableEvent")
...
function Tycoon:SubscribeTopic(topicName, callback)
local connection = self._topicEvent.Event:Connect(function(name, ...) --here is error
if name == topicName then
callback(...)
end
end)
return connection
end