I currently have a spider that when in range follows the player when the spider gets close enough to the player the spider should stop MoveTo() and do an attack. but the problem is when I call MoveTo Towards the Spider’s own humanoid RootPart to stop movement it flicks to the left side any solutions would help.
including other ways to stop movement.
I added comments where the move to part is
if anyone is wondering I already tried anchoring and un anchoring.
while wait(1) do
for _, Spider in pairs(SpiderFolder:GetChildren()) do
local Character = nil
local Magnitude = nil
local LastTargetDist = SpiderDetectDistance
local ClosestTarget = nil
if Spider.StartPos.Value == "" then
Spider.StartPos.Value = tostring(Spider.HumanoidRootPart.Position)
end
for _, Player in pairs(game.Players:GetChildren()) do
Character = game.Workspace:WaitForChild(Player.Name)
Magnitude = (Character:WaitForChild("HumanoidRootPart").Position - Spider.HumanoidRootPart.Position).Magnitude
if Magnitude < LastTargetDist then
LastTargetDist = Magnitude
ClosestTarget = Character
end
end
if ClosestTarget then
if Spider.Targeted.Value == false then
local SpiderWalk = Spider.Zombie:LoadAnimation(Spider.Walk)
local SpiderBite = Spider.Zombie:LoadAnimation(Spider.Bite)
Spider.Targeted.Value = true
Spider.HumanoidRootPart:SetNetworkOwner(game.Players:FindFirstChild(ClosestTarget.Name))
Spider.LastPosition.Value = tostring(Vector3.new(0,1000,0))
coroutine.resume(coroutine.create(function()
while RunService.Heartbeat:Wait() do
local CharacterDist = (ClosestTarget:WaitForChild("HumanoidRootPart").Position - Spider.HumanoidRootPart.Position).Magnitude
local CharacterFromSpawn = (ClosestTarget:WaitForChild("HumanoidRootPart").Position - Vector3.new(Spider.StartPos.Value:match("(.+), (.+), (.+)"))).Magnitude
local SpawnPosition = Vector3.new(Spider.StartPos.Value:match("(.+), (.+), (.+)"))
local CharacterPosition = ClosestTarget.HumanoidRootPart.Position
local LastPosition = Vector3.new(Spider.LastPosition.Value:match("(.+), (.+), (.+)"))
local PointToPoint = (CharacterPosition - LastPosition).Magnitude
if CharacterFromSpawn < SpiderDetectDistance then
if CharacterDist > 10 then --distance from spider to character
if PointToPoint > 2 then -- dont worry about this its just to moveto() is not spammed
if Spider.Walking.Value == false then
Spider.Walking.Value = true
SpiderWalk:Play()
end
Spider.Zombie:MoveTo(CharacterPosition) -- move to player
Spider.LastPosition.Value = tostring(ClosestTarget.HumanoidRootPart.Position) -- again dont worry
end
else
if Spider.Walking.Value == true then
Spider.Zombie:MoveTo(Spider.Torso.Position)
Spider.Walking.Value = false
SpiderWalk:Stop()
end
end
else
Spider.Zombie:MoveTo(SpawnPosition)
repeat
wait(.1)
until game.Workspace.Monters.Spiders.Spider.HumanoidRootPart.Velocity == Vector3.new(0,0,0)
if Spider.Walking.Value == true then
Spider.Walking.Value = false
SpiderWalk:Stop()
end
Spider.HumanoidRootPart.Position = SpawnPosition
Spider.Targeted.Value = false
break
end
end
end))
end
end
end
end