Fly script only going one way when I change my camera direction

So I made my own flying script since I am making a tycoon with a fly tool. But every time I press W or a button it does not change the direction of the player to the camera, and I have to press it again to change the direction which is bad when you are not trying to move in a straight line. I have body gyro but it does nothing to fix it.

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local HumanRootPart = character:WaitForChild("HumanoidRootPart")
local cam = game.Workspace.CurrentCamera
local Debris = game:GetService("Debris")
local UserInputS = game:GetService("UserInputService")
local pressed = false
local Wpressed = false
local Apressed = false
local Spressed = false
local Dpressed = false
local BodyVel 
local BodyGyro 
local Force = 5000

UserInputS.InputBegan:Connect(function(input, chat)
	if not chat then
		if input.KeyCode == Enum.KeyCode.Q then
			if not pressed then 
				pressed = true
				BodyGyro = Instance.new("BodyGyro")
				BodyGyro.MaxTorque = Vector3.new(1, 1, 1)*10^6;
				BodyGyro.P = 10^6;
				BodyGyro.CFrame = HumanRootPart.CFrame;
				BodyVel = Instance.new("BodyVelocity")
				BodyVel.Parent = HumanRootPart
				BodyVel.P = 2500
				BodyVel.MaxForce = Vector3.new(Force,Force,Force)
				BodyVel.Velocity = Vector3.new(0,0,0)
				--HumanRootPart.CFrame = CFrame.new(cam.CFrame.LookVector)
			elseif pressed then 
				BodyVel:Destroy()
				pressed = false
			end
		end
		if input.KeyCode == Enum.KeyCode.W then
				BodyVel.Velocity = cam.CFrame.LookVector * 65
			BodyGyro.CFrame = cam.CFrame;
			Wpressed = true
		elseif input.KeyCode == Enum.KeyCode.A then
			Apressed = true
			BodyVel.Velocity = cam.CFrame.RightVector * -65
		elseif input.KeyCode == Enum.KeyCode.S then
			Spressed = true
			BodyVel.Velocity = cam.CFrame.LookVector * -65
		elseif input.KeyCode == Enum.KeyCode.D then
			Dpressed = true
			BodyVel.Velocity = cam.CFrame.RightVector * 65
		end
	end
end)

UserInputS.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.W then
		Wpressed = false
		BodyVel.Velocity = Vector3.new(0,0,0)
	elseif input.KeyCode == Enum.KeyCode.A then
		Apressed = false
		BodyVel.Velocity = Vector3.new(0,0,0)
	elseif input.KeyCode == Enum.KeyCode.S then
		Spressed = false
		BodyVel.Velocity = Vector3.new(0,0,0)
	elseif input.KeyCode == Enum.KeyCode.D then
		Dpressed = false
		BodyVel.Velocity = Vector3.new(0,0,0)
	end
end)

I also tried a loop on the body velocity but it kept going even if I did not press the button
Any help would be welcomed.

2 Likes

You could try using GetPropertyChangedSignal for when the player’s camera CFrame changes and change the velocity based upon that. For example,

Camera:GetProeprtyChangedSignal("CFrame"):Connect(function()
    if IsFlying then
        if UserInputService:IsKeyDown(Enum.KeyCode.W) then
            BodyVel.Velocity = Camera.CFrame.LookVector * 65
        ...
        end
        if BodyGyro and BodyGyro.Parent then
            BodyGyro.CFrame = Camera.CFrame
        end
    end
end)

I hope this helped. :slight_smile:

3 Likes

I will test it later when I can. Thank you for the help!

1 Like

I have a question is there any use in body gyro for my script because I feel like I should use.

Yeah, you could use it to have the player’s character face the camera’s direction - just make sure to destroy it as well when you’re not flying. ;p

EDIT

Sure, lemme edit the code in my og reply. :slight_smile:

EDIT2

Yeah it be like that sometimes. x3 Glad to hear it’s working now. :smile:

I was trying to do that as well but it went haywire could you show me?

The face camera direction was not working before because I did not parent it. It is always the simple mistakes!

1 Like