Hi, I have a script here which calls for something from a Module Script.
However it says"Nil". This is the Module Script:
The NPC thing is set somewhere else. When I call it in any other script or such, I get the correct result, not nil. But when I call it in this script:
`local NPCDataModule = require(game.ReplicatedStorage.NPCDataModule)
local function DespawnNPC()
print(NPCDataModule.GetSpawnedNPC())
local ReplicatedStorage = game:GetService("ReplicatedStorage")
print(NPCDataModule.GetSpawnedNPC())
-- Function to find a humanoid in a model
local function findHumanoid(model)
for _, descendant in ipairs(model:GetDescendants()) do
if descendant:IsA("Humanoid") then
return descendant
end
end
end
-- Function to despawn the NPC
local function despawnCharacter(character, despawnPart)
local humanoid = findHumanoid(character)
if humanoid then
humanoid.WalkSpeed = 16
-- Move the NPC to the despawn location
humanoid:MoveTo(despawnPart.Position)
humanoid.MoveToFinished:Wait() -- Wait for the NPC to reach the despawn location
-- Despawn the NPC by removing it from the game
character:Destroy()
end
end
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local CorrectOrNotModule = require(game.ReplicatedStorage.CorrectOrNot)
local isTicketCorrect = CorrectOrNotModule.GetTicketCorrectness()
if isTicketCorrect then
local function displayChatMessageAboveHead(character, message)
local humanoid = findHumanoid(character)
if humanoid then
humanoid.WalkSpeed = 0
game:GetService("Chat"):Chat(humanoid.Parent.Head, message, Enum.ChatColor.White)
local message = "Thank you!"
displayChatMessageAboveHead(character,message)
end
end
else
local randomMessages = {
"How will I ever get to my destination?",
"I'm so done with you right now bro",
"Oh darling, are you overworked?",
"bruv how you mess up this bad?"
}
local randomIndex = math.random(1, #randomMessages)
local randomMessage = randomMessages[randomIndex]
local message = randomMessage
local function displayChatMessageAboveHead(character, message)
local humanoid = findHumanoid(character)
if humanoid then
humanoid.WalkSpeed = 0
game:GetService("Chat"):Chat(humanoid.Parent.Head, message, Enum.ChatColor.White)
displayChatMessageAboveHead(character,message)
end
end
wait(5)
end
-- Retrieve the NPC model reference from the NPCDataModule
local npcToDespawn = NPCDataModule.GetSpawnedNPC()
if npcToDespawn then
print("Despawning NPC:", npcToDespawn.Name) -- Debug line
local despawnPart = game.Workspace.Despawn
local humanoid = findHumanoid(npcToDespawn)
if humanoid then
humanoid.WalkSpeed = 0
local direction = (despawnPart.Position - humanoid.Parent.PrimaryPart.Position).unit
local stopPosition = despawnPart.Position - direction * 2
humanoid:MoveTo(stopPosition)
humanoid.MoveToFinished:Wait()
humanoid.WalkSpeed = 16
end
-- Despawn the NPC
despawnCharacter(npcToDespawn, despawnPart)
else
print("No NPC to despawn") -- Debug line
end
end
local function SpawnNPC()
local stations = {"London Waterloo", "Manchester Piccadilly", "Edinburgh Waverley", "Birmingham New Street"}
local times = {"9am", "1pm", "5pm", "8pm"}
local ticketTypes = {"One-way", "Return", "Season ticket"}
local function findHumanoid(model)
for _, descendant in ipairs(model:GetDescendants()) do
if descendant:IsA("Humanoid") then
return descendant
end
end
end
local function fadeInCharacter(model)
end
local function displayChatMessageAboveHead(character, message)
local humanoid = findHumanoid(character)
if humanoid then
humanoid.WalkSpeed = 0
game:GetService("Chat"):Chat(humanoid.Parent.Head, message, Enum.ChatColor.White)
wait(5)
humanoid.WalkSpeed = 16 --
end
end
local function moveCharacterToDestination(character, destination)
local humanoid = findHumanoid(character)
if humanoid then
local messageDisplayed = false
local function onMoveFinished(reached)
if reached and not messageDisplayed then
messageDisplayed = true
-- Randomly
local selectedStation = stations[math.random(#stations)]
local selectedTime = times[math.random(#times)]
local selectedTicketType = ticketTypes[math.random(#ticketTypes)]
local message = "Hello! I'd like a ticket to " .. selectedStation .. ", at " .. selectedTime .. ", " .. selectedTicketType
displayChatMessageAboveHead(character, message)
humanoid.MoveToFinished:Disconnect()
end
end
humanoid.MoveToFinished:Connect(onMoveFinished)
local stopDistance = 2
local direction = (destination.Position - character.PrimaryPart.Position).unit
local stopPosition = destination.Position - direction * stopDistance
humanoid:MoveTo(stopPosition)
end
end
local npcNumber = math.random(1, 6)
local npcTemplateName = "NPC_Template" .. npcNumber
local npcName = "NPC" .. npcNumber
local loadedCharacterName = npcName
local startPart = game.Workspace.start
local endPart = game.Workspace.endfunny
local npcModel = game.Workspace:FindFirstChild(npcTemplateName)
if not npcModel then
local npcTemplate = game.ReplicatedStorage:FindFirstChild(npcTemplateName)
if npcTemplate then
npcModel = npcTemplate:Clone()
npcModel.Name = npcTemplateName
npcModel.Parent = game.Workspace
else
warn("NPC template not found for " .. npcTemplateName)
end
end
local loadedCharacter = npcModel
if loadedCharacter and startPart and endPart then
fadeInCharacter(loadedCharacter)
moveCharacterToDestination(loadedCharacter, endPart)
end
-- Store the reference to the spawned NPC in the NPCDataModule
NPCDataModule.SetSpawnedNPC(npcModel)
print(npcModel)
end
local function randomWait()
local waitTime = math.random(10, 35)
wait(waitTime)
end
– Call the function to initiate the despawning process
local ReplicatedStorage = game:GetService(“ReplicatedStorage”)
local ticketPrintedEvent = ReplicatedStorage:WaitForChild(“TicketPrintedEvent2”)
ticketPrintedEvent.OnServerEvent:Connect(function(player)
print(“It was received”)
print(NPCDataModule.GetSpawnedNPC())
DespawnNPC()
randomWait()
SpawnNPC()
end)`