Continuously check lookVector

I have this script that I’m starting to make and I’m trying to make it use linear velocity to always fly in front of the character, but I don’t know how to make it continuously get what the lookVector is.

script

local Character = script.Parent
local Humanoid = Character:FindFirstChildWhichIsA("Humanoid")
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")

local UserInputService = game:GetService("UserInputService")

local Attachment = Instance.new("Attachment", HumanoidRootPart)
local Lv = Instance.new("LinearVelocity")
Lv.MaxForce = math.huge
Lv.Attachment0 = Attachment
Lv.Enabled = false
Lv.Parent = HumanoidRootPart


UserInputService.InputBegan:Connect(function(input, isTyping)
	if isTyping then return end
	if input.KeyCode == Enum.KeyCode.E then
		Lv.Enabled = true
		Lv.VectorVelocity = HumanoidRootPart.CFrame.Position + Vector3.new(0, 50, 0)
		
		task.wait(0.2)
		
		Lv.VectorVelocity = HumanoidRootPart.CFrame.lookVector * 100
	end
end)
1 Like

Use a loop.

Code:

local Character = script.Parent
local Humanoid = Character:FindFirstChildWhichIsA("Humanoid")
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")

local UserInputService = game:GetService("UserInputService")

local Attachment = Instance.new("Attachment", HumanoidRootPart)

local Lv = Instance.new("LinearVelocity")
Lv.MaxForce = math.huge
Lv.Attachment0 = Attachment
Lv.Enabled = false
Lv.Parent = HumanoidRootPart

local isEnabled = false

UserInputService.InputBegan:Connect(function(input, isTyping)
	if isTyping then return end
	if input.KeyCode == Enum.KeyCode.E and not isEnabled then
		isEnabled = true
		
		Lv.Enabled = true
		Lv.VectorVelocity = HumanoidRootPart.CFrame.Position + Vector3.new(0, 50, 0)

		task.wait(0.2)
		
		while task.wait() and isEnabled do
			Lv.VectorVelocity = HumanoidRootPart.CFrame.LookVector * 100
		end
	end
end)

Change:

isEnabled = false

To end the loop.

1 Like

I also made a version where you can toggle it:

local Character = script.Parent
local Humanoid = Character:FindFirstChildWhichIsA("Humanoid")
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")

local UserInputService = game:GetService("UserInputService")

local Attachment = Instance.new("Attachment", HumanoidRootPart)

local Lv = Instance.new("LinearVelocity")
Lv.MaxForce = math.huge
Lv.Attachment0 = Attachment
Lv.Enabled = false
Lv.Parent = HumanoidRootPart

local isEnabled = false

UserInputService.InputBegan:Connect(function(input, isTyping)
	if isTyping then return end
	if input.KeyCode == Enum.KeyCode.E then
		isEnabled = not isEnabled
		Lv.Enabled = isEnabled
		
		if isEnabled then
			Lv.VectorVelocity = HumanoidRootPart.CFrame.Position + Vector3.new(0, 50, 0)

			task.wait(0.2)

			while task.wait() and isEnabled do
				Lv.VectorVelocity = HumanoidRootPart.CFrame.LookVector * 100
			end
		end
	end
end)
1 Like

Thanks so much, you’re amazing!

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.