OOP-Based Module cannot read a "self.value" variable

  1. What do you want to achieve?
    Have a function able to read a self.value variable.

  2. What is the issue?
    It looks like a function in an oop-based module script cannot read a self.value variable.

  3. What solutions have you tried so far?
    I’ve tried re-scripting it, reading my past work, and comparing them. It’s confusing and I don’t know what I did wrong.

Here is a preview of the oop-based module:

function BoxinModule.new(gloves, config)
	--Default Variables
	local self = {}
	setmetatable(self, BoxinModule)
	self.cameras = require(script:WaitForChild("CameraModule"))
	
	--Player Variables
	self.locPlr = game.Players.LocalPlayer
	
	--Character Variables
	self.character = self.locPlr.Character or self.locPlr.CharacterAdded:Wait()
	self.humanoid = self.character:WaitForChild("Humanoid")
	
	--Variables
	self.BoxGloves = gloves
	self.BoxConfig = require(config)
	
	self.charStates = 
		{
			idle = false,
			hopping = false,
		}
	
	return self
end

function BoxinModule:PlayRandomJump()
	local pick = math.random(1, 6)
	local clone = script.JumpSounds:FindFirstChild(pick):Clone()
	
	clone.Parent = self.character:FindFirstChild("Torso") --Here it can't read "self.character" Err Code: **attempt to index nil with 'FindFirstChild'** 
	clone:Destroy()
end

return BoxinModule

How can I fix it?

Just to make sure, this module is being used strictly on the client right…?

image

The provided module is used for the Client.

Alright after initiliazing the player, print out the character and see what it prints

--Player Variable
self.locPlr = game.Players.LocalPlayer

print(self.locPlr.Character or self.locPlr.CharacterAdded:Wait())	

are you calling playrandomjump with a colon

1 Like

I printed the character right after it declared it as a “self variable”.

Works perfectly fine, but once it tries to get called in another function, it gets read as nil.

Yup, I did do that. I don’t see a difference unless I’m stupid…

Could you show the code where you’re calling the function please

function BoxinModule:PlayRandomJump()
	local pick = math.random(1, 6)
	local clone = script.JumpSounds:FindFirstChild(pick):Clone()
	
	clone.Parent = self.character:FindFirstChild("Torso")
	clone:Destroy()
end

function BoxinModule:Hop(direction)
	BoxinModule:PlayRandomJump()
	--BoxinModule:SwitchCharState("hopping")
	
	if direction then
		
	else
		
	end
	
	wait(3)
	--BoxinModule:SwitchCharState("idle")
end

um no thats defining the function, i meant where you’re actually using it

I edited my reply, go see “BoxinModule:Hop(direction)”

Did you make sure to do this at the top?

local BoxinModule = {}
BoxinModule.__index = BoxinModule

Yes, I did. It is at the top.

local BoxinModule = {}
BoxinModule.__index = BoxinModule

function BoxinModule.new(gloves, config)
	--Default Variables
	local self = {}
	setmetatable(self, BoxinModule)
	self.cameras = require(script:WaitForChild("CameraModule"))
	
	--Player Variables
	self.locPlr = game.Players.LocalPlayer
	
	--Character Variables
	self.character = self.locPlr.Character or self.locPlr.CharacterAdded:Wait()
	self.humanoid = self.character:WaitForChild("Humanoid")
	
	--Variables
	self.BoxGloves = gloves
	self.BoxConfig = require(config)
	
	self.charStates = 
		{
			idle = false,
			hopping = false,
		}
	
	return self
end

function BoxinModule:PlayRandomJump()
	local pick = math.random(1, 6)
	local clone = script.JumpSounds:FindFirstChild(pick):Clone()
	
	clone.Parent = self.character:FindFirstChild("Torso")
	clone:Destroy()
end

--Gameplay Functions
function BoxinModule:Hop(direction)
	BoxinModule:PlayRandomJump()
	--BoxinModule:SwitchCharState("hopping")
	
	if direction then
		
	else
		
	end
	
	wait(3)
	--BoxinModule:SwitchCharState("idle")
end

try this

function BoxinModule:PlayRandomJump()
	local pick = math.random(1, 6)
	local clone = script.JumpSounds:FindFirstChild(pick):Clone()
	
	clone.Parent = self.character:FindFirstChild("Torso")
	clone:Destroy()
end

function BoxinModule:Hop(direction)
	self:PlayRandomJump()
	--BoxinModule:SwitchCharState("hopping")
	
	if direction then
		
	else
		
	end
	
	wait(3)
	--BoxinModule:SwitchCharState("idle")
end

1 Like

Oh! It works! Thank you very much!

1 Like

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