Script breaking SOLELY because of the KeyCode

You can write your topic however you want, but you need to answer these questions:
I want players to be able to use every “keybind pattern” (W/A/S/D/Q/E flight).

Players (for example) normally can’t press W + A + Q to go forward, left, and down at the same time, but rebinding the Q key alone fixes the problem.

I can’t really think of any solutions to make W + A + Q compatible, what I have now is rebinding Q to something else for now.

The "broken" script
local uis = game:GetService("UserInputService")

local ws = Instance.new("IntValue")
ws.Parent = script
ws.Name = "W/S"
ws.Value = 3

local ad = Instance.new("IntValue")
ad.Parent = script
ad.Name = "A/D"
ad.Value = 3

local qe = Instance.new("IntValue")
qe.Parent = script
qe.Name = "Q/E"
qe.Value = 3

local currentactive = 0
local flying = false
local wsadqe = {ws, ad, qe}
local multi = {1, 1, 0.707, 0.577}
local plr = game.Players.LocalPlayer
local bv = Instance.new("BodyVelocity")
bv.Parent = plr.Character.PrimaryPart
bv.Name = "FlightForce"
bv.MaxForce = Vector3.new(0, 0, 0)
bv.Velocity = Vector3.new(0, 0, 0)

uis.InputBegan:Connect(function(key, chat)
	if chat then return end
	
	if key.KeyCode == Enum.KeyCode.W then
		ws.Value = 1
	end
	
	if key.KeyCode == Enum.KeyCode.S then
		ws.Value = 2
	end

	if key.KeyCode == Enum.KeyCode.D then
		ad.Value = 1
	end

	if key.KeyCode == Enum.KeyCode.A then
		ad.Value = 2
	end

	if key.KeyCode == Enum.KeyCode.E then
		qe.Value = 1
	end

	if key.KeyCode == Enum.KeyCode.Q then
		qe.Value = 2
	end

	if key.KeyCode == Enum.KeyCode.LeftAlt then
		if not flying then
			flying = true
			bv.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
		else
			flying = false
			bv.MaxForce = Vector3.new(0, 0, 0)
			bv.Velocity = Vector3.new(0, 0, 0)
		end
	end
	
end)

uis.InputEnded:Connect(function(key, chat)
	if chat then return end
	
	if key.KeyCode == Enum.KeyCode.W or key.KeyCode == Enum.KeyCode.S then
		ws.Value = 3
	end

	if key.KeyCode == Enum.KeyCode.A or key.KeyCode == Enum.KeyCode.D then
		ad.Value = 3
	end

	if key.KeyCode == Enum.KeyCode.E or key.KeyCode == Enum.KeyCode.Q then
		qe.Value = 3
	end
	
end)

while wait() do
	local currentactive = 1
	local veladds = {
		1,
		-1,
		0
	}
	
	for _, v in ipairs(script:GetChildren()) do
		if v.Value ~= 3 then
			currentactive += 1
		end
	end

	bv.Velocity = ((plr:GetMouse().Hit.LookVector * veladds[ws.Value]) + (plr:GetMouse().Hit.RightVector * veladds[ad.Value]) + (plr:GetMouse().Hit.UpVector * veladds[qe.Value])) * 50 * multi[currentactive]
end
The "working" script (very similar to broken script but every Keycode.E is Keycode.Three and every Keycode.Q is Keycode.One)
local uis = game:GetService("UserInputService")

local ws = Instance.new("IntValue")
ws.Parent = script
ws.Name = "W/S"
ws.Value = 3

local ad = Instance.new("IntValue")
ad.Parent = script
ad.Name = "A/D"
ad.Value = 3

local qe = Instance.new("IntValue")
qe.Parent = script
qe.Name = "Q/E"
qe.Value = 3

local currentactive = 0
local flying = false
local wsadqe = {ws, ad, qe}
local multi = {1, 1, 0.707, 0.577}
local plr = game.Players.LocalPlayer
local bv = Instance.new("BodyVelocity")
bv.Parent = plr.Character.PrimaryPart
bv.Name = "FlightForce"
bv.MaxForce = Vector3.new(0, 0, 0)
bv.Velocity = Vector3.new(0, 0, 0)

uis.InputBegan:Connect(function(key, chat)
	if chat then return end
	
	if key.KeyCode == Enum.KeyCode.W then
		ws.Value = 1
	end
	
	if key.KeyCode == Enum.KeyCode.S then
		ws.Value = 2
	end

	if key.KeyCode == Enum.KeyCode.D then
		ad.Value = 1
	end

	if key.KeyCode == Enum.KeyCode.A then
		ad.Value = 2
	end

	if key.KeyCode == Enum.KeyCode.Three then --it can be anything else but E
		qe.Value = 1
	end

	if key.KeyCode == Enum.KeyCode.One then --it can be anything else but Q
		qe.Value = 2
	end

	if key.KeyCode == Enum.KeyCode.LeftAlt then
		if not flying then
			flying = true
			bv.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
		else
			flying = false
			bv.MaxForce = Vector3.new(0, 0, 0)
			bv.Velocity = Vector3.new(0, 0, 0)
		end
	end
	
end)

uis.InputEnded:Connect(function(key, chat)
	if chat then return end
	
	if key.KeyCode == Enum.KeyCode.W or key.KeyCode == Enum.KeyCode.S then
		ws.Value = 3
	end

	if key.KeyCode == Enum.KeyCode.A or key.KeyCode == Enum.KeyCode.D then
		ad.Value = 3
	end

	if key.KeyCode == Enum.KeyCode.Three or key.KeyCode == Enum.KeyCode.One then
		qe.Value = 3
	end
	
end)

while wait() do
	local currentactive = 1
	local veladds = {
		1,
		-1,
		0
	}
	
	for _, v in ipairs(script:GetChildren()) do
		if v.Value ~= 3 then
			currentactive += 1
		end
	end

	bv.Velocity = ((plr:GetMouse().Hit.LookVector * veladds[ws.Value]) + (plr:GetMouse().Hit.RightVector * veladds[ad.Value]) + (plr:GetMouse().Hit.UpVector * veladds[qe.Value])) * 50 * multi[currentactive]
end

If you test this (local script ofc), you will notice that rebinding the Q/E to something else fixes the problem. Why does rebinding a key alone fix or cause problems?

Is it just my keyboard or are you also experiencing this?

2 Likes

This was before.

What keyboard do you have? You can only press so many keys at a time on any keyboard, but most gaming keyboards try to allow most three to five key combinations of common gaming keys like those to be held at the same time. They call it anti-ghosting.
Go ahead and give this a try to see if it is your keyboard.
https://www.microsoft.com/applied-sciences/projects/anti-ghosting-demo/

2 Likes

You can have 3 combinations, but some aren’t allowed, so this current post can’t fully be a repeat.

Holding W and A should cause the Z component of the humanoid’s ‘MoveDirection’ property to be ~-0.707. You could use this information in conjunction with checking if the Q key is currently pressed to determine if the desired combination of keys are being pressed. This is also compatible with other keys that correspond to movement, albeit with slightly different ‘MoveDirection’ component values.

I use the 0.707 or 0.577 rule for when 2 or 3 keys are pressed.