Making a F for Flashlight

I was making a F for Flashlight for my game and came across a bug when I press F it would fling me, Here’s the code

local Player = game:GetService("Players").LocalPlayer
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

local humanoid = Player.Character:WaitForChild("Humanoid")
local mouse = Player:GetMouse()

local lerpingSpeed = 0.5

UserInputService.InputBegan:Connect(function(input, processed)
	if processed then return end

	if input.KeyCode == Enum.KeyCode.F then
		local mainPart = Instance.new("Part", Player.Character:FindFirstChild("Head"))
		
		mainPart.Anchored = false
		mainPart.CanCollide = false
		mainPart.Size = Vector3.new(1, 1, 1)
		mainPart.Transparency = 0.5

		local weld = Instance.new("Weld", Player.Character:FindFirstChild("Head"))
		weld.Part0 = Player.Character:FindFirstChild("Head")
		weld.Part1 = mainPart

		local light = Instance.new("SpotLight", mainPart)
		light.Brightness = 10

		local targetPosition

		mouse.Move:Connect(function()
			targetPosition = mouse.Hit.p
		end)

		RunService.RenderStepped:Connect(function(deltaTime)
			if targetPosition then
				mainPart.CFrame = mainPart.CFrame:Lerp(CFrame.new(targetPosition), lerpingSpeed * deltaTime)
			end
		end)
		
	end
end)

A few things I noticed here, One being that I have found making welds at runtime doesn’t always work and has a few problems, I’m not sure if this is the issue or not but I would look at that. Second, This is a small issue and shouldn’t affect your code that much but It’s usually better practice to create the instance without a parent, set the values you desire and then parent it. This possibly could explain why your character gets flung, but by default instances are not anchored so I’m not quite sure. Give these all a few tries I’d say

mainPart.Parent = Player.Character:FindFirstChild("Head")

I tried this a while back also.
Try this … Not fully tested, but I hope it will clear up any problems.

local Player = game:GetService("Players").LocalPlayer
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

local humanoid = Player.Character:WaitForChild("Humanoid")
local mouse = Player:GetMouse()

local lerpingSpeed = 0.5

local mainPart = Instance.new("Part", Player.Character:FindFirstChild("Head"))
mainPart.Anchored = false
mainPart.CanCollide = false
mainPart.Size = Vector3.new(1, 1, 1)
mainPart.Transparency = 0.5

local weld = Instance.new("Weld", Player.Character:FindFirstChild("Head"))
weld.Part0 = Player.Character:FindFirstChild("Head")
weld.Part1 = mainPart

local light = Instance.new("SpotLight", mainPart)
light.Brightness = 10
local targetPosition

local flashlightEnabled = false

UserInputService.InputBegan:Connect(function(input, processed)
	if processed then return end

	if input.KeyCode == Enum.KeyCode.F then
		flashlightEnabled = not flashlightEnabled
		if flashlightEnabled then
			mouse.Move:Connect(function()
				targetPosition = mouse.Hit.p
			end)
		else
			targetPosition = nil
		end
	end
end)

RunService.RenderStepped:Connect(function(deltaTime)
	if targetPosition and flashlightEnabled then
		mainPart.CFrame = mainPart.CFrame:Lerp(CFrame.new(targetPosition), lerpingSpeed * deltaTime)
	end
end)

try weldconstraint, weld can be a bit wonky sometimes