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

How to detect if an input (UserInputService) matches a string?

How would I check to see if the input matches the string I set? Ty

--Server Scripter
local Key = function(plyr, key, user)
	--if user then return end
	local val = "L"
	if key.KeyCode == Enum.KeyCode[val] then
		print(val)
	end
end;

--Local
uis.InputBegan:connect(function(key, game)
	if game then return end
	event:FireServer()
end)

1 comment

0

Error: 07:17:37.988 - Workspace.Hail Alpha:84: attempt to index nil with 'KeyCode'

2 answers

The process we need to perform to complete this is called concatenation. Using the square braces, we can integrate a piece of information into a line of code with ease.

Your program however contains a few syntax and deprecation issues, so please recognize what has changed below:

local UserInputService = game:GetService("UserInputService")
local String = "S"

UserInputService.InputBegan:Connect(function(InputObject, GameProcessed)
	if (GameProcessed) then return end
	if (InputObject.UserInputType == Enum.UserInputType.Keyboard) then
	    if (InputObject.KeyCode == Enum.KeyCode[String]) then
			print(String.." was pressed!")
		end
	end
end)

4 comments

0

07:13:36.601 - Workspace.Hail Alpha:84: attempt to index nil with 'KeyCode'

0

Yeah so I forgot to fire the function that's why nothing happened because I've tried concat as well. Though this still errored. I'm pretty new to remotes and im unsure if what I did is right.

0

That is a highly unorthodox error, I'm unsure why that occurred.

0

Kindly check my edited script

What i think is wrong is that u are using a single equal "=" and if statements always require double equal "==" or double symbols so its like that

local string = "S"
	 
uis.InputBegan:connect(function(key, game)
	  if key.KeyCode == Enum.KeyCode.string then
	        print(string)
	    end
	end)

1 comment

0

There is a large difference between the assign and comparison operators, these need to be used respectfully as said by @keiujIr. Though their operand names are different, they difficult to differ sometimes as they merely separate themselves by a single duplicate symbol.

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