How to check if something is a valid member of something

I want to check if a something is a valid member of something else, I know to use if whatever:FindFirstChild(“what im looking for”) ~= nil then is a way to find if the thing im looking for exists but It does not seem to work here

local test = "MouseButton3"
local test2 = "U" -- try with both test and test2
uis.InputBegan:Connect(function(input, gpe)
	if gpe then return end
	if Enum.UserInputType[test] ~= nil and input.UserInputType == Enum.UserInputType[test] or Enum.KeyCode[test] ~= nil and input.KeyCode == Enum.KeyCode[test] then
		print("Works")
	end
end

I have tried a variation with FindFirstChild(“”) but it still comes up as an error messege and this is like the 8th variation if different checks of if enum.userinputtype == whatever

also I know it does sometimes print what its supposed to, but it also comes up with a bunch of error messages that I would like to avoid, I think I know a more messy solution that does not really solve this problem but does the same outcome, but could cause problems down the line that I could use if what im asking is simply not possible

also what im trying to do is make a way for customizable controls in the end of all this, I think I only need help with this part, I like to write my own code I just dont know a lot about code lol

The problem is that the UserInputType enum is a numerically-indexed array meaning that the key doesn’t correspond to the input.

I think you are looking for something like this:

local uis = game:GetService("UserInputService")
local test = "MouseButton3"
local test2 = "U" -- try with both test and test2

local function DoesKeyCodeExist(EnumChild)
	return pcall(function()
		return Enum.KeyCode[EnumChild] ~= nil
	end)
end

uis.InputBegan:Connect(function(input, gpe)
	if gpe then return end

	if Enum.UserInputType[test] ~= nil and input.UserInputType == Enum.UserInputType[test] or DoesKeyCodeExist(test2) and input.KeyCode == Enum.KeyCode[test2] then
		print("Works")
	end
end)

not really, I should have been more specific when I typed “try with both test and test2” because I mean they should be interchangeable when going through, so if test = “U” then it should work fine as well as if test = “MouseButton3”

I see what you mean. You would just need to duplicate the code for DoesKeyCodeExist and make a DoesUserInputTypeExist as well and instead of return Enum.KeyCode it would be return Enum.UserInputType and then change your first condition in the IF statement from Enum.UserInputType[test] ~= nil to DoesUserInputTypeExist(test).

Actually you could probably make that code more generic and accept the Enum type as a parameter as well like DoesEnumTypeExist(Enum.UserInputType, test) or DoesEnumTypeExist(Enum.KeyCode, test2). Just some thoughts.

There is this method called Enum:GetEnumItems() and it does exactly what it sounds like. So you run it first, convert the returned array into a hashmap so it’s easier to use, and then you can directly index that hashmap to check for something without any errors.

local UserInputTypeEnum: {[string]: true} = {}
for _, v in Enum.UserInputType:GetEnumItems() do
	UserInputTypeEnum[v.Name] = true
end
local KeycodeEnum: {[string]: true} = {}
for _, v in Enum.KeyCode:GetEnumItems() do
	KeycodeEnum[v.Name] = true
end

local test = "MouseButton3"
local test2 = "U"

print(UserInputTypeEnum[test] ~= nil) --> true
print(KeycodeEnum[test2] ~= nil) --> true
1 Like

thanks this is exactly what I was trying to do

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.