Help with keycode issues

Hello Devs. As me and my friend were scripting a basketball game we had ran into a problem were the keycode gives us a weird error nun of us could understand. Can you help us with this error?

Error Image:

Code:

local player = game.Players.LocalPlayer;
local character = player.Character;
local humanoid = character.Humanoid;

local rep = game:GetService("ReplicatedStorage");
local uis = game:GetService("UserInputService");

local gameRemotes = rep:WaitForChild("GameRemotes");
local values = character:WaitForChild("Values");

local cd = 1.75;
local KeysHeld = {
   S = false;
   D = false;
   A = false;
   W = false;
}
uis.InputEnded:Connect(function(input, gpe)
   if gpe then return end;
   if input.KeyCode == Enum.KeyCode.S then
   	KeysHeld.S = false
   elseif input.KeyCode == Enum.KeyCode.D then
   	KeysHeld.D = false
   elseif input.KeyCode == Enum.KeyCode.A then
   	KeysHeld.A = false
   elseif input.KeyCode == Enum.KeyCode.W then
   	KeysHeld.W = false
   end
   if not character:FindFirstChild("Ball") then return end;
   if values.Shooting.Value == false then return end;
   if input.KeyCode == Enum.KeyCode.E then
   	gameRemotes:WaitForChild("Shoot"):FireServer(false, 2, cd);
   end
end)
uis.InputBegan:Connect(function(Key,_ , input, gpe)
   if gpe then return end;

   if Key.KeyCode == Enum.KeyCode.S then
   	KeysHeld.S = true
   elseif Key.KeyCode == Enum.KeyCode.D then
   	KeysHeld.D = true
   elseif Key.KeyCode == Enum.KeyCode.A then
   	KeysHeld.A = true
   elseif Key.KeyCode == Enum.KeyCode.W then
   	KeysHeld.W = true
   end
   if not character:FindFirstChild("Ball") then return end;
   if values.Shooting.Value == true then return end;
   if input.KeyCode == Enum.KeyCode.E then
   	gameRemotes:WaitForChild("Shoot"):FireServer(true, 1, cd);
   elseif  input.KeyCode == Enum.KeyCode.E and KeysHeld.D == true and KeysHeld.A == false and KeysHeld.W == false and KeysHeld.S == false then
   	gameRemotes:WaitForChild("Shoot"):FireServer(true, 1, cd,"Left");
   
   elseif  input.KeyCode == Enum.KeyCode.E and KeysHeld.A == true and KeysHeld.D == false and KeysHeld.W == false and KeysHeld.S == false  then
   gameRemotes:WaitForChild("Shoot"):FireServer(true, 1, cd,"Right");

   elseif  input.KeyCode == Enum.KeyCode.E and KeysHeld.S == true and KeysHeld.A == false and KeysHeld.W == false and KeysHeld.D == false  then
   gameRemotes:WaitForChild("Shoot"):FireServer(true, 1, cd,"Fade");

   end
end)


1 Like

What is up with this? Change it to uis.InputBegan:Connect(function(input, gpe). InputBegan passes 2 arguments, not four. The error seems to come from this line

if input.KeyCode == Enum.KeyCode.E then

inside of .InputBegan. Currently, you defined “Key” correctly but defined “input” incorrectly, leading to it being nil and causing the error. Change all references of “Key” to “input” inside the .InputBegan function, for example

if Key.KeyCode == Enum.KeyCode.S then

if input.KeyCode == Enum.KeyCode.S then

I’ve done what you did. Now it gives me an error at a different line.

You need to change ALL uses of “Key” to “input” in .InputBegan, not just the one I gave.

According to this page, InputBegan gives 2 values


The input itself, and the gameprocessed event.

Here is a slightly modified code, that may work:

local player = game.Players.LocalPlayer;
local character = player.Character;
local humanoid = character.Humanoid;

local rep = game:GetService("ReplicatedStorage");
local uis = game:GetService("UserInputService");

local gameRemotes = rep:WaitForChild("GameRemotes");
local values = character:WaitForChild("Values");

local cd = 1.75;
local KeysHeld = {
   S = false;
   D = false;
   A = false;
   W = false;
}
uis.InputEnded:Connect(function(input, gpe)
   if gpe then return end;
   if input.KeyCode == Enum.KeyCode.S then
   	KeysHeld.S = false
   elseif input.KeyCode == Enum.KeyCode.D then
   	KeysHeld.D = false
   elseif input.KeyCode == Enum.KeyCode.A then
   	KeysHeld.A = false
   elseif input.KeyCode == Enum.KeyCode.W then
   	KeysHeld.W = false
   end
   if not character:FindFirstChild("Ball") then return end;
   if values.Shooting.Value == false then return end;
   if input.KeyCode == Enum.KeyCode.E then
   	gameRemotes:WaitForChild("Shoot"):FireServer(false, 2, cd);
   end
end)
uis.InputBegan:Connect(function(Key, gpe)
   if gpe then return end;

   if Key.KeyCode == Enum.KeyCode.S then
   	KeysHeld.S = true
   elseif Key.KeyCode == Enum.KeyCode.D then
   	KeysHeld.D = true
   elseif Key.KeyCode == Enum.KeyCode.A then
   	KeysHeld.A = true
   elseif Key.KeyCode == Enum.KeyCode.W then
   	KeysHeld.W = true
   end
   if not character:FindFirstChild("Ball") then return end;
   if values.Shooting.Value == true then return end;
   if Key.KeyCode == Enum.KeyCode.E then
   	gameRemotes:WaitForChild("Shoot"):FireServer(true, 1, cd);
   elseif  input.KeyCode == Enum.KeyCode.E and KeysHeld.D == true and KeysHeld.A == false and KeysHeld.W == false and KeysHeld.S == false then
   	gameRemotes:WaitForChild("Shoot"):FireServer(true, 1, cd,"Left");
   
   elseif  Key.KeyCode == Enum.KeyCode.E and KeysHeld.A == true and KeysHeld.D == false and KeysHeld.W == false and KeysHeld.S == false  then
   gameRemotes:WaitForChild("Shoot"):FireServer(true, 1, cd,"Right");

   elseif  Key.KeyCode == Enum.KeyCode.E and KeysHeld.S == true and KeysHeld.A == false and KeysHeld.W == false and KeysHeld.D == false  then
   gameRemotes:WaitForChild("Shoot"):FireServer(true, 1, cd,"Fade");

   end
end)

I believe the primary issue is that you copied some of the code from inputEnded into inputBegan, but didn’t change the variable names to what they should be

1 Like
local replicated = game:GetService("ReplicatedStorage")
local userInput = game:GetService("UserInputService")
local players = game:GetService("Players")
local player = players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

local gameRemotes = replicated:WaitForChild("GameRemotes")
local shoot = gameRemotes:WaitForChild("Shoot")
local values = character:WaitForChild("Values")
local shooting = values:WaitForChild("Shooting")

local keysHeld = {
	[Enum.KeyCode.W] = false,
	[Enum.KeyCode.A] = false,
	[Enum.KeyCode.S] = false,
	[Enum.KeyCode.D] = false
}

local keyMoves = {
	[Enum.KeyCode.W] = "",
	[Enum.KeyCode.A] = "Left",
	[Enum.KeyCode.S] = "Fade",
	[Enum.KeyCode.D] = "Right"
}

local keyCode = Enum.KeyCode.E

userInput.InputBegan:Connect(function(input, processed)
	if processed then
		return
	end

	for key, bool in pairs(keysHeld) do
		if input.KeyCode == key then
			keysHeld[key] = true
		end
	end
	
	local ball = character:FindFirstChild("Ball")
	if not ball then
		return
	end

	if shooting.Value then
		return
	end
	
	if input.KeyCode == keyCode then
		local keys = 0
		for key, bool in pairs(keysHeld) do
			if bool then
				keys += 1
				if keys >= 2 then
					return
				end
			end
		end
		
		for key, bool in pairs(keysHeld) do
			if bool then
				shoot:FireServer(true, 1, 1.75, keyMoves[key])
			end
		end
	end
end)

userInput.InputEnded:Connect(function(input, processed)
	if processed then
		return
	end

	for key, bool in pairs(keysHeld) do
		if input.KeyCode == key then
			keysHeld[key] = false
		end
	end
	
	local ball = character:FindFirstChild("Ball")
	if not ball then
		return
	end
	
	if not shooting.Value then
		return
	end
	
	if input.KeyCode == keyCode then
		shoot:FireServer(false, 2, 1.75)
	end
end)
1 Like

You’re still not correct, unfortunately. All areas of ‘Key’ should be changed to input and the parameters of ‘uis.InputBegan:Connect(function()’ are still incorrect.

You have it as:
‘uis.InputBegan:Connect(function(key,_ , input, gpe)’

It should be:
‘uis.InputBegan:Connect(function(input, gpe)’

That ‘key’ nonsense should be gone.