Move player forward in direction of camera

Hello, I want to make a simple script so that if a player presses a button, then they can go foward in the direction of the camera for 5 seconds

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()

local cam = workspace.CurrentCamera

local Speed = 10
local Time = 5

local connection
local function Fly()
	char.PrimaryPart.AssemblyLinearVelocity = cam.CFrame.LookVector * Speed
end

local Button : ClickDetector = "somebuttonhere"

Button.MouseClick:Connect(function(plr2)
	if plr == plr2 then
		connection = cam:GetPropertyChangedSignal("CFrame"):Connect(Fly)
		task.wait(Time)
		connection:Disconnect()
		connection = nil
	end
end)

Next time provide some code please.

i meant a key on the keyboard. sorry

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()

local cam = workspace.CurrentCamera

local UIS = game:GetService("UserInputService")

local Speed = 10
local Time = 5

local connection
local function Fly()
	char.PrimaryPart.AssemblyLinearVelocity = cam.CFrame.LookVector * Speed
end

local Key = Enum.KeyCode.E

UIS.InputBegan:Connect(function(input,gps)
	if gps then return end
	if input.KeyCode == Key and not connection then
		connection = cam:GetPropertyChangedSignal("CFrame"):Connect(Fly)
		task.wait(Time)
		connection:Disconnect()
		connection = nil
	end
end)
1 Like