__index infinite dropdown question

This hasn’t caused any issues as of yet, but I am curious as to why this is happening.

Here I am looking to see if I have all the information I need about a player in a table.

local function claimPlot(Player, Claimed:Folder)
	if PlayerDetails[Player.Name].Plot == nil then	
		PlayerDetails[Player.Name].Plot = Plot.New(PlayerDetails[Player.Name], Claimed)
		
		Claimed.Control.Tag.SurfaceGui.TextLabel.Text = Player.Name
		
		Tween:Create(Claimed.Control.Door, TweenInfo.new(1, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out), {Position = Claimed.Control.Door.Position + Vector3.new(0,10,0)}):Play()
		Claimed.Control.Door.Open:Play()
		
		return true
	end
end

The information its looking for is created by this module.

local Player = {}
Player.__index = Player

Player.dataTemplate = {
	["Rooms"] = 1
}

function Player.New(ply, Data)
	local self = setmetatable(Player,{})
	
	self.Player = ply
	self.CurrentAnim = nil
	self.Data = Data
	self.Plot = nil
	
	return self
end

My concern is when I put a breakpoint and open up the watch window this happens.

I think this a definition of a memory leak, but I am not too sure. Why does this happen?

Thank you for your time.

1 Like

Why do you even need a metatable in the first place?

Just so I can inherent the functions from the original module.

local function onPlayerAdded(Player:Player)	
	local wrap = PlayerManager.New(Player, PlayerManager.dataTemplate)
	PlayerDetails[wrap.Player.Name] = wrap
	
	if Player.Character then
		onCharacterAdded(Player.Character)
	else
		Player.CharacterAdded:Connect(onCharacterAdded)
	end
end

1 Like

It looks like you might be recursively calling the Player table through __index.

The __index should omitted when setting the metatable right? Ill take another look at the lua manual, but I am not sure how that would be possible with what I have.

You have the arguments in setmetatable flipped around. You’re actually setting the metatable on the Player object each time you call the constructor.

Change

local self = setmetatable(Player,{})

to

local self = setmetatable({}, Player) --//Now an entirely new object is created

I assume this is what’s causing some abnormal results to occur. (maybe?)

1 Like

WOW.

Yep that it! Man its always the little things. Thanks for catching that! Ill mark this down as the solution.
:sweat_smile:

1 Like

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