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

How the heck do I teleport an NPC in the Workspace somewhere?

Im only a beginner at scripting and I have no idea how teleporting something that contains a Humanoid works.

I tried moving the torso, the humanoid, using moveto? (i dug around in some free models to try to salvage that) and i cant seem to do it.

Im just very lost, and I feel like the answer is simple. Anywhere anyone can point me?

1 comment

1

You're all good to go!

2 answers

✓ Accepted answer

Using the Humanoid for :MoveTo(), I wouldn't use as it causes it to walk instead of actually teleport. Instead, target the Model. As it will provide the PrimaryPart, which should be the Instance passed to teleporting

So to solve your issue, this is what your Script should look like

local CoordianteFrame = workspace:WaitForChild("Location1").CFrame
local UserInputService = game:GetService("UserInputService") --// Random Event fire.
local Char = workspace:WaitForChild("NPC")
UserInputService.InputBegan:Connect(function(InputObject, GameProcessed)
	pcall(function()
	    if (InputObject.KeyCode == Enum.KeyCode.E and not GameProcessed) then
	        if (Char:FindFirstChild("Humanoid"):IsA("Humanoid")) then --// Same principles
	            Char:MoveTo(CoordianteFrame.Position)
		        return
	        end
	        print(Char.Name.." doesn't have a Humanoid.")
	    end
	end)
end)

Hope this sends you in the right direction, I suggest reading about :MoveTo() to get a better grasp, also UserInputService may be new to you, so check out UserInputSerivce when you can. Good Luck! lots have changed in this past year.

20 comments

0

I apologize, I feel very inept for not specifying - im attempting to move an NPC located in the workspace to a given coordinate, not a or the player. But thank you, this script is helpful for another problem I have had - I havent scripted on ROBLOX in a while in general. Not up to speed.

0

If you have a location defined by a Part, I can easily configure this answer to accommodate to your requirements

0

Char is what I named the Variable for the NPC, sorry if that was confusing:)

0

Yeah, my intention is to periodically move said NPC to certain parts (i.e. location1,2, etc) when events happen, like a click detection or a part being touched. Nothing too crazy, just simply moving the NPC around

0

Is the desired location a physical object? If so please provide the reference path

0

Yes, its a part in the workspace. game.Workspace.location1 would be the path.

0

Alright, I'll get to work

0

Take your time. I appreciate the help

0

That should do it for you?

0

The output keeps telling me that = is expected where + is on line 10. Have I not used the correct type of script, put it in the right place, or is it just an error?

0

(line 07 on what you posted)

0

Try switching the Variable Name, it could get confused?

0

Doesnt seem to be working. maybe we could talk on discord to sort this out with more ease, if youre alright with that?

0

Oh I wrote Chat instead of Char

0

my device is about to die, I can handle this in about an hour f that’s okay?

0

Tomorrow might work better, its getting late where I live. Ill message you on ROBLOX with the rest of the situation, id prefer to handle it from whatever can be established from that.

0

again, thanks for everything. greatly appreciated!

0

No problem!

0

Nice syntax error on line 7

0

That should fix everything, up a bit late last night

To teleport something with a humanoid, you need to change the CFrame value.

workspace.NPC.PrimaryPart.CFrame = CFrame.new(x, y, z)

x, y, and z are the coordinates of the destination.

Hope this helps

2 comments

0

Thank you. This helps me a lot.

0

I also was searching for an answer like this.Thank you tho.

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