Attempt to index nil with 'update_connection'

i have a module script which has an module:activate and a module:disable, with another function module:create_agent

in the create agent function, there is this line self.update_connection = nil, in the active function the update_connection gets changed to a heartbeat loop like this

	self.update_connection = RunService.Heartbeat:Connect(function(dt)
		PathfindingAgent:Update() --dt
	end)

the disable function disconnects the update connection

the problem is this line self.update_connection = RunService.Heartbeat:Connect(function(dt) causes the error in the title

this is the full code for better visualization:

function PathfindingAgent:CreateAgent(agent, agent_settings)
	local self = setmetatable({}, PathfindingAgent)

--stuff and stuff
	
	self.update_connection = nil

	--more stuff

	return self
end


function PathfindingAgent:Activate()
	self.update_connection = RunService.Heartbeat:Connect(function(dt) --this the error
		PathfindingAgent:Update() --dt
	end)

	self.is_activated = true
end


function PathfindingAgent:Disable()
	if not self.is_activated then return end

	self.update_connection:Disconnect()
	self.current_movement_state = self.Movemenet_States.Idle
end
1 Like

Have you tried :

function PathfindingAgent:Activate()
    if not RunService then
        error("RunService is nil")
    end
    -- Rest of the function
end
1 Like

thanks for replying so quick, but RunService is working fine.
do you think it could be because

i make a variable named self even though there is already a built in self??

Actually yes, The self parameter is a parameter that is automatically passed to all functions aha

Remove the local & use the built in self

1 Like

Can you show how you’re calling the function? You also didn’t set the index method for the self metatable.

2 Likes

removing it still has the same error; ill just send the full part of it @HeyWhatsHisFace

function PathfindingAgent:CreateAgent(agent, agent_settings)
	--local self = setmetatable({}, PathfindingAgent)

	self.Agent = agent
	self.Settings = agent_settings
	self.is_activated = nil

	self.movement_states = {
		InPath = 0,
		WaitingForPath = 1,
		Idling = 2,
		Dead = 3,
		Pathfinding = 4,	
		Chasing = 5,
	}

	self.status_states = {
		Activated = 0,
		DeActivated = 1,
		Contained = 2,
	}

	self.curr_movement_state = self.movement_states.Idling
	self.curr_status_state = self.status_states.DeActivated

	self.update_connection = nil

	self.Humanoid = self.Agent:WaitForChild("Humanoid")
	self.HumanoidRootPart = self.Agent:WaitForChild("HumanoidRootPart")
	self.Torso = self.Agent:WaitForChild("Torso")

	_, self.bounding_box = self.Agent:GetBoundingBox()

	for i, part in pairs(self.Agent:GetChildren()) do 
		if part:IsA("BasePart") then 
			part:SetNetworkOwner(nil) 
		end
	end

	return self
end


function PathfindingAgent:Activate()
	self.update_connection = RunService.Heartbeat:Connect(function(dt)
		PathfindingAgent:Update() --dt
	end)

	self.is_activated = true
end


function PathfindingAgent:Disable()
	if not self.is_activated then return end

	self.update_connection:Disconnect()
	self.current_movement_state = self.Movemenet_States.Idle
end

no… remove the local not the whole line

Can you show how you are calling the module functions?

realized that but same error anyway

serverscript inside NPC (this is a pathfinding module if u havent realized)


local PathfindingModule = require(RS:WaitForChild("PathfindingModule"))

local Agent_Settings = {
	1,
	30,
	50 --nil
}
PathfindingModule.Activate()
PathfindingModule.CreateAgent(BigLenny, Agent_Settings)
--get target
print(PathfindingModule.FindClosestTarget())
1 Like

You need to create the agent before activating it. You also need to set the index.

1 Like

Replace your line with this real quick

function PathfindingAgent:Disable()
    if not self.is_activated then return end

    self.update_connection:Disconnect()
    self.current_movement_state = self.movement_states.Idle
end

1 Like

doesnt fix the error but did stop a future error so gj ig

i may have set it wrong but im pretty sure it is set?

local PathfindingAgent = {}
PathfindingAgent.__index = PathfindingAgent --this is at the top of module

--inside the create agent code
self = setmetatable({}, PathfindingAgent)

could you try the run service thing for me, i just wanna see if it prints or no.

function PathfindingAgent:Activate()
    if not RunService.Heartbeat then
        error("RunService.Heartbeat is nil")
    end
    self.update_connection = RunService.Heartbeat:Connect(function(dt)
        PathfindingAgent:Update() --dt
    end)

    self.is_activated = true
end

If it doesn’t i know the issue

Other troubleshoots

function PathfindingAgent:Activate()
    if not self then
        error("self is nil")
    end
    if not self.update_connection then
        error("self.update_connection is nil")
    end
    self.update_connection = RunService.Heartbeat:Connect(function(dt)
        PathfindingAgent:Update() --dt
    end)

    self.is_activated = true
end

it does not print (error) as runservice.heartbeat is working

1 Like

^ & you could troubleshoot all fields to see if they’re set correctly

You have to make sure to use : to carry self as well.

local agent = PathfindingModule.CreateAgent(BigLenny, Agent_Settings)
agent:Activate()
print(agent:FindClosestTarget())
1 Like

works however in the module when i print(self.Agent) instead of printing BigLenny it prints the Agent_Settings instead? and when printing self.Settings it prints nil
full function code:


function PathfindingAgent:CreateAgent(agent, agent_settings)
	 self = setmetatable({}, PathfindingAgent)

	self.Agent = agent
	self.Settings = agent_settings
	self.is_activated = nil

	self.movement_states = {
		InPath = 0,
		WaitingForPath = 1,
		Idling = 2,
		Dead = 3,
		Pathfinding = 4,	
		Chasing = 5,
	}

	self.status_states = {
		Activated = 0,
		DeActivated = 1,
		Contained = 2,
	}

	self.curr_movement_state = self.movement_states.Idling
	self.curr_status_state = self.status_states.DeActivated

	self.update_connection = nil
	print(self.Agent) --SHOULD print BigLenny, instead prints settings bruh
    print(self.Settings) --prints nil bruh
	self.Humanoid = self.Agent:WaitForChild("Humanoid")
	self.HumanoidRootPart = self.Agent:WaitForChild("HumanoidRootPart")
	self.Torso = self.Agent:WaitForChild("Torso")

	_, self.bounding_box = self.Agent:GetBoundingBox()

	for i, part in pairs(self.Agent:GetChildren()) do 
		if part:IsA("BasePart") then 
			part:SetNetworkOwner(nil) 
		end
	end

	return self
end

@HeyWhatsHisFace

Sorry, I didn’t catch the : in the function declaration. The first parameter of a : function will always be self when called using .

You can change the function declaration to

function PathfindingAgent.CreateAgent(agent, agent_settings)

or change the call method to

local agent = PathfindingModule:CreateAgent(BigLenny, Agent_Settings)

It doesn’t matter which function format you use because you are redefining self anyways, as long as the call and declaration methods match.

1 Like

W mans for getting this fixed; also so weird how a simple : or . can completely change the params

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