Problem with destroying button after data loads

  1. What do you want to achieve? Keep it simple and clear!
    So basically i’m making an tycoon game. I’m using B Ricey tutorial for it. For some strange reason while i’m loading data and destroy button(Like in tutorial #8) it just gives me error with loading component , but it shouldn’t. Note this is not components problem in case it worked without despawn function and tag

  2. What is the issue? Include screenshots / videos if possible!
    Described in first, here is screenshoot:
    image

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub? I was making this by youtube video and it should work

Despawn module code:

local Despawn = {}
Despawn.__index = Despawn

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

function Despawn:Init()
	self.Tycoon:SubscribeTopic("Button", function(id)
		if id == self.Instance:GetAttribute("Id") then
			self.Instance:Destroy()
		end
	end)
end

return Despawn

Code which get error:

local CollectionService = game:GetService("CollectionService")
local template = game:GetService("ServerStorage").Template
local componentFolder = script.Parent.Components
local tycoonStorage = game:GetService("ServerStorage").TycoonStorage
local playerManager = require(script.Parent.PlayerManager)

local function NewModel(model, cframe)
	local newModel = model:Clone()
	newModel:PivotTo(cframe)
	newModel.Parent = workspace
	return newModel
end

local Tycoon = {}
Tycoon.__index = Tycoon

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

function Tycoon:Init()
	print(self._spawn)
	self.Model = NewModel(template, self._spawn.CFrame)
	self._spawn:SetAttribute("Occupied", true)
	self.Owner.RespawnLocation = self.Model.Spawn
	self.Owner:LoadCharacter()
	
	self:LockAll()
	self:LoadUnlocks()
	self:WaitForExit()
end

function Tycoon:LoadUnlocks()
	for _, id in ipairs(playerManager.GetUnlockIds(self.Owner)) do
		self:PublishTopic("Button", id)
	end
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:Unlock(instance, id)
	playerManager.AddUnlockId(self.Owner, 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() -- here is an problem
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:WaitForExit()
	playerManager.PlayerRemoving:Connect(function(player)
		if self.Owner == player then
			self:Destroy()
		end	
	end)	
end

function Tycoon:Destroy()
	self.Model:Destroy()
	self._topicEvent:Destroy()
	self._spawn:SetAttribute("Occupied", false)
end

return Tycoon

Yeah this is 7th topic already, but i have no freaking idea why i always get error on each video.

Sorry for bump, not fixed still.

Well i got new details. Script just think what Despawn component(Module) doesn’t exists(it’s just nil). I have no idea in case here it is.
image

Well i don’t know what to do with that. I already tryed to debug it, but it always just send nil even if Despawn component exists.

Also another some more info, while im adding component function

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

it says component is Despawn
But in create component function it is nil

function Tycoon:CreateComponent(instance, componentScript)
	local compModule =require(componentScript)
	local newComp = compModule.new(self, instance)
	print(newComp)
	if newComp then
		newComp:Init()
	end
end

And another small detail while i say print(componentScript.Name) it’s not nil, compModule same not nil, but new comp is nil.

Hope this helps to solve my problem

local Despawn = {}
Despawn.__index = Despawn

function Despawn.new(tycoon, instance)
	local self = setmetatable({}, Despawn)
	self.Tycoon = tycoon
	self.Instance = instance
  return self -- You need to return it, silly.
end

function Despawn:Init()
	self.Tycoon:SubscribeTopic("Button", function(id)
		if id == self.Instance:GetAttribute("Id") then
			self.Instance:Destroy()
		end
	end)
end

return Despawn

Hey it’s me again.

Edit: Also, before flooding out more topics, you can consider private messaging me with any issues and I can attempt to help you solve them if I’m available.

1 Like

Omg i’m so dumb, how i could forget about returning it…