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
local pos = mouse.Hit
--other code
plr.Character.HumanoidRootPart.CFrame = pos
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.
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.
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.