Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why doesn't this script work? Its supposed to teleport you behind a target

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.

1 answer

✓ Accepted answer

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.

6 comments

0

Btw smiling kid is a rigged character. WIll this still Apply?

0

It still hasn't worked.

0

Is there an error? What happens?

0

There is no error. it just doesn't teleport at all and nothing appears in the output. I tried debugging using prints that I put right below "if nearestEnemy then" and it Printed but the script below it still didn't work.

0

Sorry I couldn't respond earlier. It was like 3 a clock for me then.

0

Never mind! I changed it to SetPrimaryPartCFrame and it worked find with the rest of your changed script! THANKS A TON!

This discussion is preserved as it was. New answers, comments, and votes are closed.