"self" becoming the class object

Input

local Ghost = {}
Ghost.__index = Ghost

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local function SetAttributes()
	-- later
end

function Ghost.new()
	SetAttributes()
	
	self = setmetatable({}, Ghost)
	self.Body = script:WaitForChild("Ghost"):Clone()--to become something else
	self.Body.Parent = ReplicatedStorage.ClonedGhosts
	print(self.Body)
	self.Name = self.Body:GetAttribute("Name")
	self.Type = self.Body:GetAttribute("Type")
	self.Difficulty = self.Body:GetAttribute("Difficulty")
	self.Room = self.Body:GetAttribute("Room")
	self.RoomName = self.Body:GetAttribute("RoomName")
	self.Body.Position = self.Room
	print(self)
	
	return self
end

function Ghost:Hunt()
	print(self)
	print(self.Body)
	self.Body.Parent = workspace
	return self
end

function Ghost:EndHunt()
	self.Body.Parent = script
	return self
end

return Ghost

Output
image

For some weird, odd reason the self variable gets overwritten with the class functions, and I have no idea why. It’s nothing to do with attributes, those are set ahead of time.

1 Like

I suspect that you’re calling the method on the class instead of an instance.

local Ghost = require(...)
local newGhost = Ghost.new()
newGhost:Hunt() -- Right
Ghost:Hunt() -- Wrong
1 Like

I believe you’re right on this, I hadn’t made a variable for the new ghost.
It’s been a while since I used LuaU.

1 Like

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