NPC Not Teleporting?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?
    I want to achieve a side companion in my game that teleports to the player when it touches a part. The NPC is local, has no collision with the local player and follows the player.

  2. What is the issue?
    He won’t teleport back to me. No matter what. I have tried and tried. Watch the video.

  3. What solutions have you tried so far?
    What I have tried is using Character:GetPrimaryPartCFrame() and Noob:SetPrimaryPartCFrame(CFrame), which worked but the players CFrame didn’t update.

It goes to where the player spawned at first when the server started.

local camera = workspace:WaitForChild("Camera")
local Noob = camera:WaitForChild("Noob")
local Player = game.Players.LocalPlayer
local PCFrame = game.Workspace:WaitForChild(tostring(Player.Name)).PrimaryPart.CFrame

Noob["Right Leg"].Touched:Connect(function(hit)
	if hit.Name == "Sandy Bottom" and hit:IsA("BasePart") then
		print("Hit sandy.")
		Noob:SetPrimaryPartCFrame(PCFrame)
	end
end)

Noob["Left Leg"].Touched:Connect(function(hit)
	if hit.Name == "Sandy Bottom" and hit:IsA("BasePart") then
		print("Hit sandy.")
		Noob:SetPrimaryPartCFrame(PCFrame)
	end
end)

I got no errors when the code ran.

How would I fix it? It could be fixed if the PrimaryPart’s CFrame updates in the player’s character. I don’t understand the difference that much of CFrame and Position.

I set the CFrame using Position and now its working.

Hey, I know the issue is fixed already, but I would just like to recall what was the problerm and how you could do it using CFrame. So basically, the only mistake you made was that you got the CFrame once when the game starts, therefore it teleported the NPC to the starting location. So, if you want to use CFrame, you can do this:

local camera = workspace:WaitForChild("Camera")
local Noob = camera:WaitForChild("Noob")
local PL = game:GetService("Players")
local Player = PL.LocalPlayer


Noob["Right Leg"].Touched:Connect(function(hit)
	if hit.Name == "Sandy Bottom" and hit:IsA("BasePart") then
		print("Hit sandy.")
        local PCFrame = Player.Character.PrimaryPart.CFrame
		Noob:SetPrimaryPartCFrame(PCFrame)
	end
end)

Noob["Left Leg"].Touched:Connect(function(hit)
	if hit.Name == "Sandy Bottom" and hit:IsA("BasePart") then
		print("Hit sandy.")
        local PCFrame = Player.Character.PrimaryPart.CFrame
		Noob:SetPrimaryPartCFrame(PCFrame)
	end
end)

For extra clarity, I would suggest using a function so you don’t have to have same code on two events. Hope it helps :innocent:.

1 Like