Trouble with flying system

Im making a flying system. all was working well but for some reason, i can only move downward

I used a video for reference since im new to flight systems

heres the video:

heres the scripts i made

–Client

local player = game.Players.LocalPlayer
local char = script.Parent
local UIS = game:GetService("UserInputService")
--------------------------
--Bool Values
local flying = false
local wPressed = false
local aPressed = false
---
local sPressed = false
local dPressed = false
local debounce = false
---
local up = false
local down = false
local toggle = false
--------------------------
local event = game.ReplicatedStorage.FlightEvent
local camera = workspace.CurrentCamera
-------------------------------------
--User Input
UIS.InputBegan:Connect(function(input, chat)
	if chat then return end
	
	if input.KeyCode == Enum.KeyCode.Q and debounce == false then
		debounce = true --Cooldown
		if toggle == false then --Toggle flight on and off
			toggle = true
			flying = true
			print("flight on")
			event:FireServer("EnabledFlight")
		else
			toggle = false
			flying = false
			print("flight off")
			event:FireServer("DisabledFlight")
		end
		task.wait(.05)
		debounce = false
	end
	
	if input.KeyCode == Enum.KeyCode.W then
		wPressed = true
		print("W true")
	elseif input.KeyCode == Enum.KeyCode.A then
		aPressed = true
		print("A trye")
	elseif input.KeyCode == Enum.KeyCode.S then
		sPressed = true
		print("S true")
	elseif input.KeyCode == Enum.KeyCode.D then
		dPressed = true
		print("D true")
	elseif input.KeyCode == Enum.KeyCode.Space then
		up = true
		print("up false")
	elseif input.KeyCode == Enum.KeyCode.LeftControl then
		down = true
		print("down true")
	end
end)

UIS.InputEnded:Connect(function(input, chat)
	if chat then return end
	
	if input.KeyCode == Enum.KeyCode.W then
		wPressed = false
		print("W false")
	elseif input.KeyCode == Enum.KeyCode.A then
		aPressed = false
		print("A false")
	elseif input.KeyCode == Enum.KeyCode.S then
		sPressed = false
		print("S false")
	elseif input.KeyCode == Enum.KeyCode.D then
		dPressed = false
		print("D false")
	elseif input.KeyCode == Enum.KeyCode.Space then
		up = false
		print("Up false")
	elseif input.KeyCode == Enum.KeyCode.LeftControl then
		down = false
		print("Down false")
	end
end)

while task.wait() do
	if flying then
		--------------
		--W
		if wPressed == true then
			local cframe = camera.CFrame
			event:FireServer("W", cframe.LookVector)
		elseif wPressed == false then
			event:FireServer("idled")
		end
		--------------
		--A
		if aPressed == true then
			local cframe = camera.CFrame
			event:FireServer("A", cframe.RightVector)
		elseif aPressed == false then
			event:FireServer("idled")
		end
		--------------
		--S
		if sPressed == true then
			local cframe = camera.CFrame
			event:FireServer("S", cframe.LookVector)
		elseif sPressed == false then
			event:FireServer("idled")
		end
		--------------
		--D
		if dPressed == true then
			local cframe = camera.CFrame
			event:FireServer("D", cframe.RightVector)
		elseif dPressed == false then
			event:FireServer("idled")
		end
		--------------
		--Up
		if up == true then
			local cframe = camera.CFrame
			event:FireServer("Up", cframe.UpVector)
		elseif up == false then
			event:FireServer("idled")
		end
		--------------
		--Down
		if down == true then
			local cframe = camera.CFrame
			event:FireServer("Down", cframe.UpVector)
		elseif down == false then
			event:FireServer("idled")
		end
	end
end

–Server

local RS = game:GetService("ReplicatedStorage")
local event = RS.FlightEvent
local BV

event.OnServerEvent:Connect(function(plr, args, pos)
	local char = plr.Character
	local HRP = char.HumanoidRootPart
	
	if args == "EnabledFlight" then
		BV = Instance.new("BodyVelocity", HRP)
		BV.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
		BV.P = 2500
		
		BV.Velocity = Vector3.new(0,0,0)
		BV.Name = "Flight Controller"
	elseif args == "DisabledFlight" then
		BV:Destroy()
	elseif args == "idled" then
		BV.Velocity = Vector3.new(0,0,0)
	elseif args == "W" then
		print("foward")
		BV.Velocity = pos * 50
	elseif args == "A" then
		print("left")
		BV.Velocity = -pos * 50
	elseif args == "S" then
		print("back")
		BV.Velocity = -pos * 50
	elseif args == "D" then
		print("right")
		BV.Velocity = pos * 50
	elseif args == "Up" then
		print("up")
		BV.Velocity = pos * 50
	elseif args == "Down" then
		print("down")
		BV.Velocity = -pos * 50
	end
end)

help would be apreciated greatly! the script is relatively simple so im not sure where the problem is exactly

thank you for your time :slightly_smiling_face:

2 Likes

Switch all of them to ifs instead of elseifs.

1 Like

this didnt work, i tried replacing the entire scripts elseifs

I fixed it

Client:

local player = game.Players.LocalPlayer
local char = script.Parent
local UIS = game:GetService("UserInputService")
--------------------------
--Bool Values
local flying = false
local wPressed = false
local aPressed = false
---
local sPressed = false
local dPressed = false
local debounce = false
---
local up = false
local down = false
local toggle = false
--------------------------
local event = game.ReplicatedStorage.FlightEvent
local camera = workspace.CurrentCamera
-------------------------------------
--User Input
UIS.InputBegan:Connect(function(input, chat)
	if chat then return end

	if input.KeyCode == Enum.KeyCode.Q and debounce == false then
		debounce = true --Cooldown
		if toggle == false then --Toggle flight on and off
			toggle = true
			flying = true
			print("flight on")
			event:FireServer("EnabledFlight")
		else
			toggle = false
			flying = false
			print("flight off")
			event:FireServer("DisabledFlight")
		end
		task.wait(.05)
		debounce = false
	end

	if input.KeyCode == Enum.KeyCode.W then
		wPressed = true
		print("W true")
	elseif input.KeyCode == Enum.KeyCode.A then
		aPressed = true
		print("A trye")
	elseif input.KeyCode == Enum.KeyCode.S then
		sPressed = true
		print("S true")
	elseif input.KeyCode == Enum.KeyCode.D then
		dPressed = true
		print("D true")
	elseif input.KeyCode == Enum.KeyCode.Space then
		up = true
		print("up false")
	elseif input.KeyCode == Enum.KeyCode.LeftControl then
		down = true
		print("down true")
	end
end)

UIS.InputEnded:Connect(function(input, chat)
	if chat then return end

	if input.KeyCode == Enum.KeyCode.W then
		wPressed = false
		print("W false")
	elseif input.KeyCode == Enum.KeyCode.A then
		aPressed = false
		print("A false")
	elseif input.KeyCode == Enum.KeyCode.S then
		sPressed = false
		print("S false")
	elseif input.KeyCode == Enum.KeyCode.D then
		dPressed = false
		print("D false")
	elseif input.KeyCode == Enum.KeyCode.Space then
		up = false
		print("Up false")
	elseif input.KeyCode == Enum.KeyCode.LeftControl then
		down = false
		print("Down false")
	end
end)

while task.wait() do
	if flying then
		--------------
		--W
		if wPressed == true then
			local cframe = camera.CFrame
			event:FireServer("W", cframe.LookVector)
		elseif wPressed == false then
			event:FireServer("idled","Forward")
		end
		--------------
		--A
		if aPressed == true then
			local cframe = camera.CFrame
			event:FireServer("A", cframe.RightVector)
		elseif aPressed == false then
			event:FireServer("idled","Left")
		end
		--------------
		--S
		if sPressed == true then
			local cframe = camera.CFrame
			event:FireServer("S", cframe.LookVector)
		elseif sPressed == false then
			event:FireServer("idled","Back")
		end
		--------------
		--D
		if dPressed == true then
			local cframe = camera.CFrame
			event:FireServer("D", cframe.RightVector)
		elseif dPressed == false then
			event:FireServer("idled","Right")
		end
		--------------
		--Up
		if up == true then
			local cframe = camera.CFrame
			event:FireServer("Up", cframe.UpVector)
		elseif up == false then
			event:FireServer("idled","Up")
		end
		--------------
		--Down
		if down == true then
			local cframe = camera.CFrame
			event:FireServer("Down", cframe.UpVector)
		elseif down == false then
			event:FireServer("idled","Down")
		end
	end
end

Server:

local RS = game:GetService("ReplicatedStorage")
local event = RS.FlightEvent
local BV
local Pressing = false


local tbl = {
	Up = false,
	Down = false,
	Forward = false,
	Right = false,
	Left = false,
	Back = false	
}


function CheckPressing()
	for i,v in pairs(tbl) do
		if v  then
			return true
		end
	end
	return false
end


event.OnServerEvent:Connect(function(plr, args, pos)
	local char = plr.Character
	local HRP = char.HumanoidRootPart
	
	if args == "idled" then
		tbl[pos] = false
	end
	
	if not CheckPressing() and BV then
		BV.Velocity = Vector3.new(0,0,0)
	end

	if args == "EnabledFlight" then
		BV = Instance.new("BodyVelocity", HRP)
		BV.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
		BV.P = 2500
		BV.Velocity = Vector3.new(0,0,0)
		BV.Name = "Flight Controller"
	elseif args == "DisabledFlight" then
		BV:Destroy()
	elseif args == "W" then
		print("foward")
		tbl.Forward = true
		BV.Velocity = pos * 50
	elseif args == "A" then
		print("left")
		tbl.Left = true
		BV.Velocity = -pos * 50
	elseif args == "S" then
		print("back")
		tbl.Back = true
		BV.Velocity = -pos * 50
	elseif args == "D" then
		print("right")
		tbl.Right = true
		BV.Velocity = pos * 50
	elseif args == "Up" then
		print("up")
		tbl.Up = true
		BV.Velocity = pos.Unit * 50
		print(BV.Velocity)
	elseif args == "Down" then
		print("down")
		tbl.Down = true
		BV.Velocity = -pos * 50
	end
end)
2 Likes