function module.DistanceFromKill() local nearestEnemy = nil local nearestRoot = math.huge workspace.SmilingKidWithRope.SmilingKid.Parent = workspace local root = workspace.SmilingKid.HumanoidRootPart for i, v in pairs(workspace.Game.People:GetChildren()) do local plrRoot = v:FindFirstChild("HumanoidRootPart") if plrRoot and plrRoot ~= root then local mag = (plrRoot.Position - root.Position).Magnitude if mag < 10 and mag < nearestRoot then nearestEnemy = plrRoot nearestRoot = mag end end end if nearestEnemy then root.CFrame = nearestEnemy.CFrame * CFrame.new(0, 0, 3) end end
Please help. This is my second time posting this and it would be appreciated.
By using CFrame.new(0,0,3), you are adding 3 to the CFrame on the z-axis. In this instance, you will only teleport behind the enemy if they are parallel to the z-axis. What you should try instead, is use the lookvector of the enemy and teleport behind them that way. A CFrame's lookvector is a directional vector with a magnitude of 1 that tells you where the front-surface of a part is facing.
function module.DistanceFromKill() local nearestEnemy = nil local nearestRoot = math.huge workspace.SmilingKidWithRope.SmilingKid.Parent = workspace local root = workspace.SmilingKid.HumanoidRootPart for i, v in pairs(workspace.Game.People:GetChildren()) do local plrRoot = v:FindFirstChild("HumanoidRootPart") if plrRoot and plrRoot ~= root then local mag = (plrRoot.Position - root.Position).Magnitude if mag < 10 and mag < nearestRoot then nearestEnemy = plrRoot nearestRoot = mag end end end if nearestEnemy then local newpos = nearestEnemy.Position local lookvector = nearestEnemy.CFrame.lookVector root.CFrame = CFrame.new(newpos - lookvector * 3, newpos + lookvector) end end
This will teleport them behind the enemy as well as have them face where the enemy is facing.