Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why Won't This Script For Pressing a Specific Key with UserInputService Work?

This script should fire when a variety of keys (which are listed in the script) are pressed, given that the "control" value is true and a tank with the player's name was able to be found. If the key being pressed is not of the given keys, it prints "false key." However, when I press a key that is of the given bunch, it still prints false key - regardless of any key I press, it prints false key. Why is this?

local UserInputService = game:GetService("UserInputService")

player = game.Players.LocalPlayer
control = player:WaitForChild("Controlling", 120)

local function KeyDown(input, gameProcessed)
	if control.Value == true then --The control value, when set to true, will switch the player's control from his character to the tank. (That is not in this script, I'm just explaining for what control is for)
		local findTank = game.Workspace.Tanks:FindFirstChild(player.Name)
		if findTank then --Find the tank that belongs to the player
			if input.UserInputType == Enum.UserInputType.Keyboard then --Only if it's a keyboard input.
				print("key pressed")
				-- w = 119, a = 97, s = 115, d = 100, q = 113, e = 101, r = 114, f = 102
				local keys = {119, 97, 115, 100, 113, 101, 114, 102}
				local properKey = nil
				for i, v in pairs(keys) do
					if input.KeyCode == v then
						properKey = v
						print(v.. " pressed") --If the key is of the given list, 
						break
					end
				end
				if properKey ~= nil then
					--Stuff happens
				else
					print("false key")
				end
			end
		else
			control.Value = false --This shouldn't be set to true if there's no tank!
		end
	end
end

UserInputService.InputBegan:connect(KeyDown)

Any and all help is highly appreciated!

5 comments

1

In the table, try using Enum.KeyCode.[insert key here]. That may work.

0

It threw an error.

0

This is what I typed in (short version to save space) local keys = {Enum.KeyCode.[119], Enum.KeyCode.[97], Enum.KeyCode.[113], Enum.KeyCode.[101]}

1

Stop indexing Enum with numbers. Also looping through Enum is extremely inefficient. Like Chip said, simply type if input.KeyCode == Enum.KeyCode.X (for example)

0

Cabbler, this worked; you have my undying gratitude :D

1 answer

You should not be using UserInputService it has been basically replaced by ContextActionService

This example is compatible with FilteringEnabled

Try putting this in a LocalScript in game.StarterPlayer.StarterPlayerScripts Edit accordingly

-- Make variables for the local player and ContextActionService
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local contextActionService = game:GetService("ContextActionService")



function Keys(Name, UserInputState, InputObject)
	game.Workspace.GetKeys:FireServer(Name, UserInputState, InputObject)
	--print(Name)
end

contextActionService:BindAction("ActionName", Keys, true, Enum.KeyCode.["EnumKey"])
contextActionService:BindAction("E", Move, true, Enum.KeyCode.E)
contextActionService:BindAction("LeftShift", Move, true, Enum.KeyCode.LeftShift)

if game:GetService("UserInputService").TouchEnabled == true then
	--Optionally add touch buttons
	--You could remove all this
	contextActionService:SetPosition("ActionName", UDim2.new(0, 0, 0.65, 0))
	contextActionService:SetPosition("E", UDim2.new(0.65, 0, 0.65, 0))
	contextActionService:SetPosition("LeftShift", UDim2.new(1.30, 0, 0.65, 0))
	--Add images to buttons
	contextActionService:SetImage("E", "13890074")
	contextActionService:SetImage("LeftShift", "13890047")
end

And put this in a Script in game.workspace Edit accordingly

local GKS = Instance.new("RemoteEvent")
GKS.Name = "GetKeys"
GKS.Parent = workspace
GKS.OnServerEvent:connect(function(Player, Name, UserInputState, InputObject)
	if UserInputState == Enum.UserInputState.Begin then
		print(Player.Name.." just started input for: "..Name)
	elseif UserInputState == Enum.UserInputState.End then
		print(Player.Name.." just ended input for: "..Name)
	end
end)

Name is the name of the Action for example "E" and "LeftShift" you can then apply any function to an user input.

ContextActionService also supports touch pads, game controllers and more

This discussion is preserved as it was. New answers, comments, and votes are closed.