Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 4 years ago
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

Log in to vote
0
Answered by
Sparks 534 Moderation Voter
4 years ago
Edited 4 years ago

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.

0
Btw smiling kid is a rigged character. WIll this still Apply? Infuriashon 17 — 4y
0
It still hasn't worked. Infuriashon 17 — 4y
0
Is there an error? What happens? Sparks 534 — 4y
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. Infuriashon 17 — 4y
View all comments (2 more)
0
Sorry I couldn't respond earlier. It was like 3 a clock for me then. Infuriashon 17 — 4y
0
Never mind! I changed it to SetPrimaryPartCFrame and it worked find with the rest of your changed script! THANKS A TON! Infuriashon 17 — 4y
Ad

Answer this question