Monster not moving full way and not disappearing when daytime?

So I made a script where a monster goes around walking to invisible parts if it is not near the player. And I also want this monster to disappear when the sun is up and appear at night. But the script printed no errors and the monster doesn’t move the full way to the part, it just goes in the direction of the part then about halfway changes directions. And the monster doesn’t disappear during day. Here’s the script:

local lighting = game:GetService("Lighting")
local clockTime = lighting.ClockTime
local Monster = game.Workspace.Monster
local humanoid = Monster.Humanoid
local destination = game.Workspace.FollowPart1
local destination2 = game.Workspace.FollowPart2
local destination3 = game.Workspace.FollowPart3
local destination4 = game.Workspace.FollowPart4
local destination5 = game.Workspace.FollowPart5

while true do
	wait()
	if clockTime >= 20 or clockTime <= 8 then 
		script.Parent.HumanoidRootPart.Position = Vector3.new(519.6, -87, -92.55)
	else
		repeat
			humanoid:MoveTo(destination.Position)
			humanoid.MoveToFinished:Wait(0.5)
			humanoid:MoveTo(destination2.Position)
			humanoid.MoveToFinished:Wait(0.5)
			humanoid:MoveTo(destination3.Position)
			humanoid.MoveToFinished:Wait(0.5)
			humanoid:MoveTo(destination4.Position)
			humanoid.MoveToFinished:Wait(0.5)
			humanoid:MoveTo(destination5.Position)
			humanoid.MoveToFinished:Wait(0.5)
		until clockTime >=20 or clockTime <= 8
	end
end

Hiwi! o/

Well. You are experiencing that issue, because MoveTo() has a 8 seconds timeout. When the Humanoid is not able to reach the goal position in 8 seconds, the Monster will stop and its choosing the next destination. (lets say the Monster is kinda lazy to fully complete its task xD

A way to avoid that is, feeding the MoveTo() with the same vector before the 8 seconds occurs.
Theres a nice example of how to do it on the MoveTo() hub documentation. But you will need to make many adaptations to make it work as you want to.

I made a little walking system for you to check how to do it. Using a different approach.

You will call the moveTo() custom function, which ask for the Humanoid, the target and a function that selects the next destination.
I placed all your destination parts into a folder called Positions in workspace, so you dont have to hardcode each destination manually. A for loop will find all parts inside the folder, and store them into an array called destinations = {}. And by using a function choosing the next one after the MoveToFinished event is triggered. So, you just need to put as many parts into the folder as you want, doesnt matter the name, just the order in which has been added to the folder.

Then, a spawned loop will be feeding the MoveTo() each 5 seconds, before the 8 secs timeout occurs. When its daylight, that thread will be stoped by another while loop function, which will check the clockTime each 2 secs (you can change those intervals as you need it).

When its night, that loop will stop and let the original thread to continue, using the previous target position got from the array. I left many comments on the script for you to see the approach.

I will attach a roblox file if you have any doubts about the hierarchy. The scripts are placed into ServerScriptService. (I added a day/night cycle too, just to test the Humanoid)

local lighting = game:GetService("Lighting")

local Monster = game.Workspace.Monster
local humanoid = Monster.Humanoid

--[[ Put as many parts into this folder in workspace to have many destinations
doesnt matter the part's name, only the order in which has been added to the folder ]]
local DestinationsFolder = game.Workspace.Positions

-- This array will be populated with all the positions
local destinations = {}

-- The loop to get all positions in folder and add it to the destionations = {} array
for _, d in ipairs(DestinationsFolder:GetChildren()) do
	table.insert(destinations, d)
end

-- Holder to know which position is the next one
local currentPos = 1

-- The main Target holder to tell the Humanoid where to go
local target = destinations[currentPos]

-- Bool for Night and Day, Night = true Day = false
local currentTime

-- Checking time during daylight
local function checkDayLight()
	while true do
		if lighting.ClockTime >= 20 or lighting.ClockTime <= 8 then
			-- Night
			--print("night, continue with the thread")
			break
		end
		-- Checking interval		
		wait(2)
	end
end

-- Main function to be called when u want to start the humanoid walk
local function moveTo(humanoid, targetPoint, nextPos)
	local targetReached = false

	-- listen for the humanoid reaching its target
	local connection
	connection = humanoid.MoveToFinished:Connect(function(reached)
		--print("reached")
		targetReached = true
		connection:Disconnect()
		connection = nil
		if nextPos then
			nextPos()
		end
	end)

	-- start walking
	humanoid:MoveTo(targetPoint.Position)
	
	--[[  Spawn a loop to refresh the walking vector. The wait is on 5 seconds to avoid the
	8 seconds timeout of WalkTo(). 5 seconds could cause a little delay on checking day/night
	(nothing important), but make it lower if you care about it ]]
	spawn(function()
		while not targetReached do
			if lighting.ClockTime >= 20 or lighting.ClockTime <= 8 then
				-- Night
			else
				-- Day
				--[[ Disconnect MoveToFinished in order to send the humanoid to its own position to stop it
				without triggering next position function]]
				connection:Disconnect()
				connection = nil
				humanoid:MoveTo(Monster.HumanoidRootPart.Position)
				
				-- Pause the thread while checking daylight
				checkDayLight()
				
				-- print("continue with the walking")
				-- Create connection again
				connection = humanoid.MoveToFinished:Connect(function(reached)
					targetReached = true
					connection:Disconnect()
					connection = nil
					if nextPos then
						nextPos()
					end
				end)
			end
			
			-- checking if humanoid still exist
			if not (humanoid and humanoid.Parent) then
				break
			end

			-- mantain walking, before the 8 seconds timeout occurs
			humanoid:MoveTo(targetPoint.Position)
			wait(5)
		end
		
		--print("disconnect MoveToFinished when spawn while loop stops")
		if connection then
			connection:Disconnect()
			connection = nil
		end
	end)
end

-- Choosing next destination function, from the array made with the folder's parts
local function nextOne()
	currentPos += 1
	if currentPos > #destinations then
		currentPos = 1
	end

	target = destinations[currentPos]
	moveTo(humanoid, target, nextOne)
end


wait(9) -- Dummy wait in order to not start the humanoid walk instantly when sever scripts starts

--[[ Trigger this function however you want, by a clickDetector, a guiButton, 
an event on ur game, on server scripts starts as right now is doing, anything you want
just be sure to send the humanoid, the target got from the array and the function what to do 
after the goal is reached ]]
moveTo(humanoid, target, nextOne)

The file:
HumanoidWalk NPC.rbxl (40.8 KB)

Hope it helps :3

2 Likes

To make the monster disappear immediately you have to use a coroutine and a flag (isNight) to stop the coroutine when it is daytime. To avoid the MoveTo time limit just add more intermediate FollowParts.

local lighting = game:GetService("Lighting")
local Monster = game.Workspace.Monster
local humanoid = Monster.Humanoid
local destination1 = game.Workspace.FollowPart1
local destination2 = game.Workspace.FollowPart2
local destination3 = game.Workspace.FollowPart3
local destination4 = game.Workspace.FollowPart4
local destination5 = game.Workspace.FollowPart5

Monster.Parent = nil
local isNight

function moveMonster()
	coroutine.wrap(function ()
		while isNight do
			humanoid:MoveTo(destination1.Position)
			humanoid.MoveToFinished:Wait()
			humanoid:MoveTo(destination2.Position)
			humanoid.MoveToFinished:Wait()
			humanoid:MoveTo(destination3.Position)
			humanoid.MoveToFinished:Wait()
			humanoid:MoveTo(destination4.Position)
			humanoid.MoveToFinished:Wait()
			humanoid:MoveTo(destination5.Position)
			humanoid.MoveToFinished:Wait()
		end
	end)()
end

while true do
	wait()
	if lighting.ClockTime < 20 and lighting.ClockTime >= 8 then
		--print("Sun")
	else
		Monster.Parent = workspace
		isNight = true
		moveMonster()
		repeat
			wait()
		until lighting.ClockTime < 20 and lighting.ClockTime >= 8
		Monster.Parent = nil
		isNight = false
	end
end
1 Like