Teleport tool teleports inside of parts

Hello! I use a teleportation tool, and it works fine for the most part, untillll you get TPed inside of a part, which happens very often. How would I make it so the player doesn’t get teleported inside of a part ( Already made a “stuck?” button but it happens way too often and it’s annoying)

Here is the code I use to teleport the player :slight_smile:

local pos = mouse.Hit
--other code
plr.Character.HumanoidRootPart.CFrame = pos

Here is a video to better explain my issue :

Where you click, you will teleport,

this is ok

the problem is:

I want to teleport ON the plataform (above)

then i believe, you need some knowledge on this:

Character.HumanoidRootPart.Position = Vector3.new -- this is just an example
1 Like

Use :MoveTo(position) on the character model. MoveTo moves a model, but spawns the model above the part if a part is in the way.

local pos = mouse.Hit.p -- p is the position of mouse hit
plr.Character:MoveTo(pos) -- moves the model. If a part is in the way, the model will spawn above it.

Let me know if you have any questions, this should solve your issue.

1 Like

Thank you! It helped and now it works!

1 Like
local players = game:GetService("Players")
local player = players.LocalPlayer or players.PlayerAdded:Wait()
local character = player.Character or player.CharacterAdded:Wait()
local mouse = player:GetMouse()

mouse.Button1Down:Connect(function()
	local pos = mouse.Hit --cframe value
	character:SetPrimaryPartCFrame(mouse.Hit)
end)

I’d use “mouse.Hit” instead of “mouse.Hit.p” and “SetPrimaryPartCFrame” instead of “MoveTo” as this uses a CFrame value instead of a Vector3 value meaning that orientation is retained.

Thanks for your reply, but this still glitches the player through buildings, what Infinite_Visions suggested to me works perfectly.

Oh my bad, I didn’t realise you didn’t want the model to clip into other stuff, I thought you were just trying to move it.

Have you considered looking into PVInstance:PivotTo?

Model:SetPrimaryPartCFrame has been superseded by PVInstance:PivotTo which acts as a more performant replacement and does not change your code’s behavior. Use PVInstance:PivotTo for new work and migrate your existing Model:SetPrimaryPartCFrame calls when convenient.

Thanks for your reply, but this still glitches the player through buildings, what Infinite_Visions suggested to me works perfectly.

That’s because MoveTo doesn’t ignore collisions, like SetPrimaryPart and PivotTo does.
More information: Model | Roblox Creator Documentation