Help with module script

I was working an a game and ran into this issue in a module script

Part of module script with issue
pcall(function()
    print(10.3)
    self.Config:Destroy()
    self.Config = nil
    print(10.33, self.Config)
end)

Output:
image

for those of you who didn’t get the issue, even after assigning self.Config to nil, it still prints ServerInfo(previously stored value) and not nil. Any help would be apricated :smiley:

Edit: The object in the game is getting destroyed but idk what’s up with this script

1 Like

The code is a part of function Match:End()
I have even tried this and got similar output

Match:End()
Match.Config = nil
print(Match.Config) -- prints ServerInfo
1 Like

Can you provide more context on what Configs is or what it does?

1 Like

sure, Config is a variable that holds an instance (Configuration).

function MatchSystem.New()
    local self = setmetatable({}, MatchSystem)
    --Step0--
    MatchSystem.Config = Instance.new("Configuration", game.ReplicatedStorage)
    MatchSystem.Config.Name = "ServerInfo"

    MatchSystem.Config:SetAttribute("Step", 0)
    game.ReplicatedStorage.RemoteEvent:FireAllClients("New Match", MatchSystem.Config)

    for i, v in pairs(game.Workspace:GetChildren()) do
        if not table.find(Dictionary.WorkspaceDefault, v) and not v:FindFirstChild("Humanoid") then
            v:Destroy()
        end
    end

    task.wait(1)

    return self
end
1 Like

Well, that’s odd. I can’t spot anything obvious that’s wrong. Have you tried replicating the bug in another project or script?

I believe its due to the property being inside the metatable and not the object.

Try this:

function MatchSystem.New()
    local self = setmetatable({}, MatchSystem)
    --Step0--
    self .Config = Instance.new("Configuration", game.ReplicatedStorage)
    self .Config.Name = "ServerInfo"

@Twexdy and @dthecoolest, I don’t think that’s the issue in this case.
I tried adding another variable MatchSystem.IDK = false and tried to change it in Match:End() function (self.IDK = true), that worked completely fine. So ya it works for other variables and stuffs but not for MatchSystem.Config.

Well then here is my reproduction code that says it should resolve the issue.

local MatchSystem = {}
MatchSystem.__index = MatchSystem

function MatchSystem.New()
	local self = setmetatable({}, MatchSystem)
	--Step0--
	self.Config = Instance.new("Configuration", game.ReplicatedStorage)
	self.Config.Name = "ServerInfo"
	
	MatchSystem.Config2 = Instance.new("Configuration", game.ReplicatedStorage)
	MatchSystem.Config2.Name = "ServerInfo"

	return self
end

local match = MatchSystem.New()
print(match.Config) --Server Info
match.Config = nil
print(match.Config) --nil

print(match.Config2) --ServerInfo
match.Config2 = nil
print(match.Config2)--ServerInfo

image

Your reproduction/testing code has a flaw in which that is not how __index works.

__index will check the object first before checking the MetaTable.

It will check first match.IDK and then check MatchSystem.

Therefore MatchSystem should still have false in its property, while the object has true for the same index leaving the original unchanged.

1 Like

I was unable to replicate the bug myself but I did notice that you’re using a way to make classes that might be causing the issue. Try doing this instead:

Class = {}
Class.__index = Class

function Class.new()
    local self = setmetatable({}, module.Result)
    -- code stuff
end

Edit:
Seems like @dthecoolest said it first lol

well I was not aware of this, from the sources I have learnt, they thought me not to use self and instead use table name instead. Thanks anyways.

actually my intention was to make a class itself, I was just using module script. Thanks for your help tho.

1 Like

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