-
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
-
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
-
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)