How to make teleport ability, without make player to stuck inside building

Hello!
I tried to make teleport ability.
But when player trying to use it
When people move their mouse to building, floor, etc… They will got teleport to there
and stuck inside it

This is the video: For devforum

and
This is script i used. (Only teleport section)

plr.Character.HumanoidRootPart.CFrame = plr.Character.HumanoidRootPart.CFrame + mouse.LookVector * math.clamp((plr.Character.HumanoidRootPart.Position - mouse.p).magnitude,1,500)

Thanks in advance.
Sorry for bad english…

Oh boy, I had the exact same question a little while back, I was trying to make a teleporting movement ability.

Try this:

local UIS = game:GetService("UserInputService")
local plr = game.Players.LocalPlayer
local distance = 500
local abletoteleport = true
local mouse = plr:GetMouse()

UIS.InputBegan:Connect(function(i)
     if i.KeyCode == Enum.KeyCode.F then
          if abletoteleport then
              abletoteleport = false
              if mouse.Target:IsA("Model") then
                  print("Please selected an open place")
              else
                  plr.Character.HumanoidRootPart.CFrame = plr.Character.HumanoidRootPart.CFrame + mouse.LookVector * math.clamp((plr.Character.HumanoidRootPart.Position - mouse.p).magnitude,1,distance) -- The code that you provide here.
              end

               
          end
          wait(3)
          abletoteleport = true
     end
end)

This is something you can do here with a local script, NOT A SERVER SCRIPT.