User input detecting same keys but they're different

This is a local script

local w,a,s,d,up,dn
local inputS=game:getService("UserInputService")
local hover = workspace.Hover
local ignition = hover.Valuess.Ignition
inputS.InputBegan:connect(function(input)
		local code=input.KeyCode
		if code==Enum.KeyCode.W or code==Enum.KeyCode.Up then
			w=true
		elseif code==Enum.KeyCode.A or code==Enum.KeyCode.Left then
			a=true
		elseif code==Enum.KeyCode.S or code==Enum.KeyCode.Down then
			s=true
		elseif code==Enum.KeyCode.D or code==Enum.KeyCode.Right then
			d=true
		elseif code==Enum.KeyCode.E or upgui.InputBegan then
			up=true
		elseif code==Enum.KeyCode.Q or  dngui.InputBegan then
			dn=true
		end
	end)

inputS.InputEnded:connect(function(input)
		local code=input.KeyCode
		if code==Enum.KeyCode.W or code==Enum.KeyCode.Up then
			w=false
		elseif code==Enum.KeyCode.A or code==Enum.KeyCode.Left then
			a=false
		elseif code==Enum.KeyCode.S or code==Enum.KeyCode.Down then
			s=false
		elseif code==Enum.KeyCode.D or code==Enum.KeyCode.Right then
			d=false
		elseif code==Enum.KeyCode.E or upgui.InputEnded then
			up=false
		elseif code==Enum.KeyCode.Q or dngui.InputEnded then
			dn=false
		end
	end)

hover.Valuess.Ignition.Changed:Connect(function()
	wait(1)
	while ignition  do
		wait()
		print(up) --prints true
		if up then
			local rotatedCFrame = CFrame.Angles(math.rad(1), 0, 0)
			hover.CFrame = hover.CFrame:ToWorldSpace(rotatedCFrame)
			print("This was activated for some reason")
                        print(dn) --prints nil
		
		elseif dn then
			local negrotatedCFrame = CFrame.Angles(-0.0174533, 0, 0)
			hover.CFrame = hover.CFrame:ToWorldSpace(negrotatedCFrame)
			print("Here we go")
		end
	end
end)

When I press Q, up is true and “this was activated for some reason” prints. When I press E, up is true and “this was activated for some reason” prints. Why?

(Also additionally, if anyone knows why, when ignition becomes false the while loop never stops. So if there’s any obvious mistake in the code please do point it out)

You’re incorrectly using upgui.InputBegan and dngui.InputBegan. Instead those should be different functions:

upgui.InputBegan:Connect(function()
	up = true 
end)
dngui.InputBegan:Connect(function()
	dn = true 
end)

upgui.InputEnded:Connect(function()
	up = false
end)
dngui.InputEnded:Connect(function()
	dn = false
end)

and they must be removed from your if statements.

1 Like