SOLVED
Hello! There is already a post regarding this but they have not solved it. What happens is that when I want to make one part face another part, it works fine most of the time but there are other times where the part looking is teleported to extremely far away positions (-30000000000000000000000000000000 or something like that). Is there any solution for this? I leave my code here:
function MoveToPart(destination)
local tweenInfo = TweenInfo.new(.2)
local tween1 = game.TweenService:Create(bot.PrimaryPart, tweenInfo, {CFrame = CFrame.new(mover.Position, Vector3.new(destination.Position.X,mover.Position.Y,destination.Position.Z))})
tween1:Play()
tween1.Completed:Wait()
wait(.1)
local tweenInfo = TweenInfo.new(math.random(1,3))
local tween1 = game.TweenService:Create(bot.PrimaryPart, tweenInfo, {CFrame = CFrame.new(Vector3.new(destination.Position.X,bot.PrimaryPart.Position.Y,destination.Position.Z)) * mover.CFrame.Rotation})
tween1:Play()
tween1.Completed:Wait()
end
--//Variables
local botsFolder = script.Parent.Parent
local movePartsFolder = botsFolder.MoveParts
local bot = script.Parent
local mover = bot.Mover
local flowModel = botsFolder.Parent.Flow
--//Functions
function IsDestinationValid(part0,part1)
if part0 and part1 then
if (part0.Position - part1.Position).magnitude < 200 then
local whitelistedParts = {
flowModel.AntiBotsCollide
}
local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Whitelist
params.FilterDescendantsInstances = whitelistedParts
local ray = workspace:Raycast(part0.Position, part1.Position - part0.Position, params)
if ray and ray.Instance then
return false
else
return true
end
else
return false
end
else
return true
end
end
function MoveToPart(destination)
local tweenInfo = TweenInfo.new(.2)
local tween1 = game.TweenService:Create(bot.PrimaryPart, tweenInfo, {CFrame = CFrame.lookAt(mover.Position, Vector3.new(destination.Position.X,mover.Position.Y,destination.Position.Z))})
tween1:Play()
tween1.Completed:Wait()
wait(.1)
local tweenInfo = TweenInfo.new(math.random(1,3))
local tween1 = game.TweenService:Create(bot.PrimaryPart, tweenInfo, {CFrame = CFrame.new(Vector3.new(destination.Position.X,bot.PrimaryPart.Position.Y,destination.Position.Z)) * mover.CFrame.Rotation})
tween1:Play()
tween1.Completed:Wait()
end
--//Main Code
while wait() do
local randomPart = movePartsFolder:GetChildren()[math.random(1,#movePartsFolder:GetChildren())]
repeat
print("Trying to find a valid part...")
wait()
randomPart = movePartsFolder:GetChildren()[math.random(1,#movePartsFolder:GetChildren())]
until IsDestinationValid(mover,randomPart) == true
MoveToPart(randomPart)
end