What do you want to achieve?
Get the NPC to move to the part in workspace
What is the issue?
The NPC just stands still and doesn’t move, it isn’t anchored.
What solutions have you tried so far?
None, I added print statements to every line and it prints all of them except for the one after humanoid:MoveTo(workspace.Map.Rooms.TestRoom.Spawn.Position), there is nothing in the output.
local humanoid = script.Parent:FindFirstChildOfClass("Humanoid")
function roaming()
local pathFindingService = game:GetService("PathfindingService")
local rootPart = humanoid.Parent.HumanoidRootPart
local path = pathFindingService:CreatePath()
path:ComputeAsync(rootPart.Position, workspace.Map.Rooms.TestRoom.Spawn.Position)
local waypoints = path:GetWaypoints()
for i, waypoint in pairs(waypoints) do
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait(2)
end
humanoid:MoveTo(workspace.Map.Rooms.TestRoom.Spawn.Position)
end
roaming()
looking at your script, it doesnt appear to print anything
Edit:
Try this:
local humanoid = script.Parent:FindFirstChild("Humanoid")
function roaming()
local pathFindingService = game:GetService("PathfindingService")
local rootPart = humanoid.Parent.HumanoidRootPart
local path = pathFindingService:CreatePath()
path:ComputeAsync(rootPart.Position, workspace.Map.Rooms.TestRoom.Spawn.Position)
local waypoints = path:GetWaypoints()
for i, waypoint in pairs(waypoints) do
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait(2)
end
humanoid:MoveTo(workspace.Map.Rooms.TestRoom.Spawn.Position)
end
while wait() do
roaming()
end
local humanoid = script.Parent.Humanoid
local pathFindingService = game:GetService("PathfindingService")
function roaming()
local rootPart = humanoid.Parent.HumanoidRootPart
local path = pathFindingService:CreatePath()
path:ComputeAsync(rootPart.Position, workspace.SpawnLocation.Position)
local waypoints = path:GetWaypoints()
for i, waypoint in pairs(waypoints) do
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait(2)
end
humanoid:MoveTo(workspace.SpawnLocation)
end
while wait() do
roaming()
end
I think the issue is because I have another while loop already in the script so the one that keeps running the function doesn’t work. I didn’t provide the full script in the original post but do you know how I could optimize this so it would work?
local players = game:GetService("Players")
local gameMain = game.ServerScriptService.GameMain
local status = {
--[[1]] "Idling", -- The ghost stays still
--[[2]] "Roaming", -- The ghost picks a random point within a defined sphere around it, then checks if the pathfinding distance is less than 5 or 10 metres. If successful, the ghost walks to that point, else it repeats the steps
--[[3]] "Interaction", -- The ghost interacts with an object nearby
--[[4]] "GhostEvent", -- The ghost performs a ghost event
--[[5]] "Hunt", -- The ghost initiates a hunt
--[[6]] "Ability" -- The ghost uses its ability, if any
}
local humanoid = script.Parent:FindFirstChildOfClass("Humanoid")
local currentStatus = 0
function setActivity(number) -- Sets the activity in the truck to the given number 0-10
gameMain.Variables.GhostActivity.Value = number
end
function ghostVisiblity(value)
if value == true then
for i, part in pairs(script.Parent:GetChildren()) do
if part:IsA("BasePart") then
part.Transparency = 0
end
end
end
end
function idling()
setActivity(0)
end
function roaming()
local isRoaming = false
spawn(function()
isRoaming = true
setActivity(math.random(5, 7))
wait(20)
isRoaming = false
setActivity(0)
end)
repeat
local pathFindingService = game:GetService("PathfindingService")
local rootPart = humanoid.Parent.HumanoidRootPart
local path = pathFindingService:CreatePath()
path:ComputeAsync(rootPart.Position, workspace.Map.Rooms.TestRoom.Spawn.Position)
local waypoints = path:GetWaypoints()
for i, waypoint in pairs(waypoints) do
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait(2)
end
humanoid:MoveTo(workspace.Map.Rooms.TestRoom.Spawn.Position)
until isRoaming == false
end
print("attempted to roam")
while wait() do
roaming()
end
function initiateHunt()
end
function ghostEvent()
end
while wait(gameMain.Variables.AverageSanityLoss.Value / 3) do
if gameMain.Variables.AverageSanityLoss.Value >= 95 then
spawn(function()
wait(15)
repeat wait() until currentStatus == status[1]
if gameMain.Variables.AverageSanity.Value <= 40 then
local randomNumber = math.random()
if randomNumber >= 0.5 then
setActivity(10)
initiateHunt()
else
ghostEvent()
end
else
local randomStatus = math.random(1, #status)
if randomStatus == status[1] then
currentStatus = randomStatus
idling()
elseif randomStatus == status[2] then
currentStatus = randomStatus
roaming()
end
end
end)
else
spawn(function()
repeat wait() until currentStatus == status[1]
if gameMain.Variables.AverageSanity.Value <= 40 then
local randomNumber = math.random()
if randomNumber >= 0.5 then
setActivity(10)
initiateHunt()
else
ghostEvent()
end
else
local randomStatus = math.random(1, #status)
if randomStatus == status[1] then
currentStatus = randomStatus
idling()
elseif randomStatus == status[2] then
currentStatus = randomStatus
roaming()
end
end
end)
end
end
if gameMain.Variables.AverageSanity.Value <= 40 then
local randomNumber = math.random()
if randomNumber >= 0.5 then
setActivity(10)
initiateHunt()
else
ghostEvent()
end
else
local randomStatus = math.random(1, #status)
if randomStatus == status[1] then
currentStatus = randomStatus
idling()
elseif randomStatus == status[2] then
currentStatus = randomStatus
roaming()
end
The math.random isnt getting anything due to being no minimum or maximum amount
while wait() do -- added the while loop here, works fine
roaming()
end
-- below this may be your issue
while wait(gameMain.Variables.AverageSanityLoss.Value / 3) do
if gameMain.Variables.AverageSanityLoss.Value >= 95 then
spawn(function()
wait(15)
repeat wait() until currentStatus == status[1]
if gameMain.Variables.AverageSanity.Value <= 40 then
local randomNumber = math.random()
if randomNumber >= 0.5 then
setActivity(10)
initiateHunt()
else
ghostEvent()
end
else
local randomStatus = math.random(1, #status)
if randomStatus == status[1] then
currentStatus = randomStatus
idling()
elseif randomStatus == status[2] then
currentStatus = randomStatus
roaming()
end
end
end)
else
spawn(function()
repeat wait() until currentStatus == status[1]
if gameMain.Variables.AverageSanity.Value <= 40 then
local randomNumber = math.random()
if randomNumber >= 0.5 then
setActivity(10)
initiateHunt()
else
ghostEvent()
end
else
local randomStatus = math.random(1, #status)
if randomStatus == status[1] then
currentStatus = randomStatus
idling()
elseif randomStatus == status[2] then
currentStatus = randomStatus
roaming()
end
end
end)
end
end
yep, after testing again, it this is the only thing that doesnt work
yeah, the script works, its just that chunk that doesnt work, hope i helped
Edit:
You usually wait until the loop is done or you break the loop by adding break into it, or you call the functions inside the loop