You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
I want to move a humanoid.
What is the issue? Include screenshots / videos if possible!
MoveTo() makes it so the enemy walks in place for about 1-2 seconds.
Here’s my code (noob is just the enemy model):
local players = game.Players:GetChildren()
local config = noob.config
local rs = game:GetService("RunService")
rs.Stepped:Connect(function()
local shortestDistancePlr = {math.huge,"Nobody"}
for i,v in pairs(players) do
local plr = workspace:FindFirstChild(v.Name)
if plr then
local magn = (plr.HumanoidRootPart.Position - noob.HumanoidRootPart.Position).Magnitude
if magn <= shortestDistancePlr[1] then
shortestDistancePlr = {magn, v.Name}
end
end
end
if shortestDistancePlr[2] ~= "Nobody" and shortestDistancePlr[1] <= config.aggro.Value * 2 then
noob.Humanoid:MoveTo(workspace:FindFirstChild(shortestDistancePlr[2]).HumanoidRootPart.Position)
end
end)
It looks like PivotTo() can only teleport, but I want it to move like an actual player to the location. I’m trying to make an enemy for a game, if that helps.
MoveTo works, but not very well. I found out why it didn’t work sometimes but I think I’ll have to explain what I mean by MoveTo working weirdly.
Once you get in the activation range of the enemy, it walks in place for about 1-2 seconds, teleports where it was supposed to move to, and works normally from there.
Here’s my code:
local players = game.Players:GetChildren()
local config = noob.config
local rs = game:GetService("RunService")
rs.Stepped:Connect(function()
local shortestDistancePlr = {math.huge,"Nobody"}
for i,v in pairs(players) do
local plr = workspace:FindFirstChild(v.Name)
if plr then
local magn = (plr.HumanoidRootPart.Position - noob.HumanoidRootPart.Position).Magnitude
if magn <= shortestDistancePlr[1] then
shortestDistancePlr = {magn, v.Name}
end
end
end
if shortestDistancePlr[2] ~= "Nobody" and shortestDistancePlr[1] <= config.aggro.Value * 2 then
noob.Humanoid:MoveTo(workspace:FindFirstChild(shortestDistancePlr[2]).HumanoidRootPart.Position)
end
end)
It’s possible that the teleporting is caused by the ownership of the NPC changing from the server to the player
To fix this simply call SetNetworkOwner() and provide nil as it’s argument this will prevent the NPC’s parts from changing ownership thus fixing the teleporting.
Keep in mind that SetNetworkOwner() is for BaseParts so you would need to iterate through every BasePart in the NPC’s model
task.wait() -- This is necessary for some reason
for _,Part in ipairs(noob:GetChildren()) do
if Part:IsA("BasePart") and Part:CanSetNetworkOwnership() then
Part:SetNetworkOwner(nil)
end
that most likely means that the NPC is trying to detect players every time regardless if it chases someone or not, I would recommend to make the NPC not check for players if it detects a player and the player is in it’s range