Click to teleport script problem

Hi, I’m trying to do a click to teleport script, but when I teleport the character goes down a little bit.

Script:

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local UserInputService = game:GetService("UserInputService")
local Settings = player.PlayerGui:WaitForChild("Settings")

player.CharacterAdded:Connect(function(char)
	character = char
end)

UserInputService.TouchTapInWorld:Connect(function(position, processedByUI)
	if processedByUI then return end
	local Enabled = Settings:GetAttribute("ClickToTeleport")
	if Enabled then
	local unitRay = workspace.CurrentCamera:ScreenPointToRay(position.X, position.Y)
	local mouseRay = Ray.new(unitRay.Origin, unitRay.Direction * 1000)
	local raycastParams = RaycastParams.new()
	raycastParams.IgnoreWater = false
	local raycastResult = workspace:Raycast(mouseRay.Origin, mouseRay.Direction, raycastParams)
	if raycastResult and character:FindFirstChild("HumanoidRootPart") then
			character.HumanoidRootPart.CFrame = CFrame.new(raycastResult.Position)
		end
	end
end)

UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if gameProcessedEvent then return end
	local Enabled = Settings:GetAttribute("ClickToTeleport")
	if Enabled then
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		local mouseLocation = UserInputService:GetMouseLocation()
		local unitRay = workspace.CurrentCamera:ScreenPointToRay(mouseLocation.X, mouseLocation.Y)
		local mouseRay = Ray.new(unitRay.Origin, unitRay.Direction * 1000)
		local raycastParams = RaycastParams.new()
		raycastParams.IgnoreWater = false
		local raycastResult = workspace:Raycast(mouseRay.Origin, mouseRay.Direction, raycastParams)
		if raycastResult and character:FindFirstChild("HumanoidRootPart") then
				character.HumanoidRootPart.CFrame = CFrame.new(raycastResult.Position)
			end
		end
	end
end)

You could use :MoveTo(raycastResult.Position) on the player character, so it will make sure that character is safe and will not teleport into block

1 Like