Script doesn't work correctly

  1. What do you want to achieve? So I’m doing a script where it tries to initialize a ModuleScript in order to make the NPC move

  2. What is the issue? It doesn’t even print that it was successful, it doesn’t warn that it failed either, this means it doesn’t even “Init”, the print after i call the Init thing doesn’t even print too

  3. What solutions have you tried so far? i didn’t find any post from others that could help

I went crazy in putting many prints to find what’s happening but its too hard for me

local ServerStorage = game:GetService("ServerStorage")

local ServerModules = ServerStorage:WaitForChild("Modules")
local ModulesAIFolder = ServerModules:WaitForChild("AI")

local character
local existingCharacter = workspace.Map.Characters.Killers:WaitForChild(script.Parent.Name, 12.5)

if existingCharacter then
	character = existingCharacter
else
	warn("Character did not appear within the expected time.")
	return
end

local isCharacterChecked = false

local function checkCharacter(object)
	if not isCharacterChecked then 
		if not object then
			warn("No character found.")
			return
		end
		
		if not object:IsA("Model") then
			warn(object.Name, "is not a model.")
			return
		end
		
		if not object.PrimaryPart then
			warn("No primary part found for", object.Name .. ".")
			return
		end
		
		isCharacterChecked = true
		print(object.Name, "is now checked.")
	else
		warn(object.Name, "is already checked.")
	end
end

if character then
	checkCharacter(character)
else
	warn("Character is nil, cannot proceed with AI folder lookup.")
	return
end

local AIFolder = ModulesAIFolder:FindFirstChild(character.Name)

if not AIFolder then
	warn("No AI Folder for", character.Name)
	return
end

local AIHandlerFolder = AIFolder:WaitForChild("Handler")
local MovementModule = AIHandlerFolder:WaitForChild("Movement")

local self = require(MovementModule)
print("MovementModule loaded:", self)
print("self.Init exists:", self.Init ~= nil)

local function handleInitialize()
	local MAX_ATTEMPTS = 50
	local CURRENT_ATTEMPTS = 0
	
	local MAIN_SUCCESS = false
	local INIT_FAILED = nil
	
	while not MAIN_SUCCESS and INIT_FAILED ~= true and CURRENT_ATTEMPTS < MAX_ATTEMPTS do
		local success, err = pcall(function()
			self.Init()
			print("Calling self.Init() for", character.Name)
		end)
		
		if success then
			INIT_FAILED = false
			MAIN_SUCCESS = true
			print("Successfully initialized AI for", character.Name)
		else
			warn("Failed to initialize AI for", character.Name, ":", err)
			CURRENT_ATTEMPTS += 1
			task.wait(0.5)
		end
	end
	
	if CURRENT_ATTEMPTS >= MAX_ATTEMPTS and INIT_FAILED == nil then
		INIT_FAILED = true
		error("Failed to initialize AI for " .. character.Name .. ", maximum attempts reached: " .. CURRENT_ATTEMPTS .. "/" .. MAX_ATTEMPTS)
	end
end

if existingCharacter and character then
	handleInitialize()
else
	warn("Character is nil, cannot proceed with AI initialization.")
	return
end

local function onCharacterAdded(child)
	if child.Name == script.Parent.Name then
		checkCharacter(child)
		handleInitialize()
	end
end
workspace.Map.Characters.Killers.ChildAdded:Connect(onCharacterAdded)

I can’t find any problems with your script, but are you sure you placed your script within ServerScriptService.

yes, i placed this script in serverscriptservice

That part, does the print, or any of the print statements after, ever show up? If not, then that suggests that your code did not even reach that point and just yielded when you require(MovementModule).

The require function will run the code inside the module script and put whatever it returned in self back in the main script. What might be happening, is something in your module script either errored or yielded, leading to the main script doing the same.

I checked today and… after putting prints in there i found the issue, it was stopping because of a function, i decided to remove that and it worked, thanks!

1 Like