Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can i fix "ServerScriptService.Tycoon:74: attempt to index nil with 'Event'"?

Asked by
s0nWay 0
2 years ago

How can i fix that? i swear to god i'm losing my mind (The error is in the line 74)

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
    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()
    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

1 answer

Log in to vote
0
Answered by
appxritixn 2235 Moderation Voter Community Moderator
2 years ago

Okay, this question took me a minute but I believe I have solved your issue.

The issue comes in Tycoon.new(player).

Instead of defining a variable that has a metatable as its value, you could (and should) just return the metatable with all the necessary variables defined within the table:

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

I hope this makes sense. If it doesn't, feel free to reach out to me here

Ad

Answer this question