Attempt to call a nil value when refering to a module script

Currently, I am trying to create a function that loops forever that handles some actions I need to happen consistently.

Originally, I had a coroutine to do this but it always ended up hanging and I do not understand why (No error message). I decided to take this out of the coroutine as I did not need it but I wanted it just in case I needed any BindableEvents or whatever.

However the problem still occurs at the same line. It now gives me the following error:

Attempt to call a nil value

The function in the module script never runs, I have tried running the function in the module script through the console and it works fine. I want to know what I could do to fix this. A snippet of my code relating to the problem is bellow.

local ServerScriptService = game:GetService("ServerScriptService")
local aiHandler = require(ServerScriptService.UnitHandler)
local serverCharacter = script.Parent.Character
local worldCharacter = serverCharacter.worldCharacter.Value
local unitInfo = 12
local isAlive = true
local target = nil

function mainLoop()
	while isAlive do
		print("Loop Start")
		if target == nil then
			print("not fighting")
			target = aiHandler.CheckTargets(worldCharacter,unitInfo)
			print(target)
		else
			-- A lot more code here, I don't believe it's related to the problem
		end
	task.wait(0.1)
	end
end
-- 2 other functions defined here
mainLoop()

A lot of my code here is missing, reason being it is 150 lines and heavily relies on lots of other systems, I feel like trying to explain it all is not going to help with the problem.

Any help will be greatly appreciated

1 Like

Found the error, one small typo, it should have been CheckTarget not CheckTargets

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