OOP not working due to "missing objects" what's wrong?

Dear Devforum,
I’ve been creating a Fife similar to B&I and G&B’s Fife however it doesn’t seem to work as I thought it would:

MODULE:

local Fife = {}
Fife.__index = Fife

function Fife.New(player : Player, tool : Tool)
	local self = setmetatable({}, Fife)
	
	self.Tool = tool
	self.Handle = self.Tool:WaitForChild("Handle")
	self.Event = self.Tool:WaitForChild("Event")

	self.State = nil
	print(self.State)
	
	self.Character = player.Character
	self.Animator = self.Character:WaitForChild("Humanoid").Animator
	self.Animations = self.Tool:WaitForChild("Animations")
	self.IdleAnimation = self.Animator:LoadAnimation(self.Animations:WaitForChild("Idle"))
	self.SelectedSong = nil
	
	return self
end

local function PlayAnimation(v : Animation)
	if v then
		v:Play()
	else
		warn("Animation does not exist.")
	end
end

local function StopAnimation(v : Animation) 
	if v then
		v:Stop()
	else
		warn("Animation does not exist.")
	end
end

function Fife:Activate(v : Sound)
	if self.SelectedSong then
		self.SelectedSong = v
		self.SelectedSong:Play()
	else
		warn("No song selected.")
	end
	
	self.State = "Playing"
	print(self.State)
	PlayAnimation(self.IdleAnimation)
end

function Fife:Deactivate()
	if self.SelectedSong then
		self.SelectedSong:Stop()
	else
		warn("No song selected.")
	end
	
	self.State = nil
	print(self.State)
	StopAnimation(self.IdleAnimation)
end


return Fife

LOCAL SCRIPT:

--Services:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SoundService = game:GetService("SoundService")
local Players = game:GetService("Players")

--Modules:
local Fife_Handler = require(ReplicatedStorage.Modules:WaitForChild("Fife_Handler"))

--Variables:
local Player = Players.LocalPlayer 
local Tool = script.Parent
local Audio = Tool:FindFirstChild("Music")

Tool.Equipped:Connect(function()
	Fife_Handler.New(Player, Tool)
end)

Tool.Activated:Connect(function()
	Fife_Handler:Activate(Audio.Fife_1)
end)

Tool.Unequipped:Connect(function()
	Fife_Handler:Deactivate()
end)

ERRORS:
Screenshot 2024-12-27 140625

why are you calling the methods of the Module? You should make an object first and call the methods of that object like this.

local Fife_Handler_Module = require(ReplicatedStorage.Modules:WaitForChild("Fife_Handler"))

local fifeHandler = Fife_Handler_Module.New()
fifeHandler:Activate(Audio.Fife_1)
fifeHandler:Deactivate()

Thanks… I honestly don’t understand why it’s like this though :sweat_smile:
You were missing 2 parameters in the FifeHandler by the way (client/Player and Tool).

The only problem I’m currently having is the song not playing:
Screenshot 2024-12-27 141624

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