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

Teleport Effect While NPC is Chasing?

I want to figure out how to give an NPC the ability to randomly glitch in and out during the course of it chasing a player. Like a ghost type effect where it disappears and reappears. The thing is that I only know how to make something teleport to a fixed location, but what I want is it to be following you and teleport say 2 or 3 steps forward. Is there any way to accomplish that?

1 answer

✓ Accepted answer

You can teleport the NPC forward by calculating the CFrame in front of it by a certain distance:

local function teleport(character, forwardDistance)
    local rootPart = character:FindFirstChild("HumanoidRootPart")
    if rootPart then
        local destinationCFrame =
            rootPart.CFrame:toWorldSpace(CFrame.new(0,0,-forwardDistance))
        rootPart.CFrame = destinationCFrame
    end
end

-- Every 2 seconds, teleport an NPC forward by 5 studs
local npc = game.Workspace.NonPlayerCharacter -- A character model
while wait(2) do
    teleport(npc, 5)
end

Edit: For more information about how the CFrame calculation works, check out this Wiki page.

2 comments

0

Thanks. I will try this when i log on

0

Worked Perfectly. Thanks

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