I want the player to be able to control an NPC by clicking around, but for some reason, the npc does not consistently follow the mouse. In this gif I am clicking rapidly, but the npc does not work as intended.
I’ve tried strictly setting the WalkToPoint to the mouse’s position, I’ve also tried using the Humanoid:MoveTo() function and currently I am using a method by ROBLOX I found on the wiki.
local function moveTo(humanoid, targetPoint, andThen)
local targetReached = false
-- listen for the humanoid reaching its target
local connection
connection = humanoid.MoveToFinished:Connect(function(reached)
targetReached = true
connection:Disconnect()
connection = nil
if andThen then
andThen()
end
end)
-- start walking
humanoid:MoveTo(targetPoint)
-- execute on a new thread so as to not yield function
spawn(function()
while not targetReached do
-- does the humanoid still exist?
if not (humanoid and humanoid.Parent) then
break
end
-- has the target changed?
if humanoid.WalkToPoint ~= targetPoint then
break
end
-- refresh the timeout
humanoid:MoveTo(targetPoint)
wait(6)
end
-- disconnect the connection if it is still connected
if connection then
connection:Disconnect()
connection = nil
end
end)
end)
event.OnClientEvent:Connect(function(t)
if t[1] == "Control" then
local npc = t[2]
local shum = npc:FindFirstChild("Humanoid")
local move = mouse.Button1Down:Connect(function()
if mouse.Hit ~= nil then
moveTo(shum, mouse.Hit.p)
end
end)
end
end