How to find key to press from a string value?

hello, what if I had a local script with a string value in it, how would i make it so that in the script if the player presses the key thats in the string value, it will do something?

you can use InputBegan

it returns all the player input which can also get the keycode pressed by the player

example:

local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(Input, Event)
	print(Input.KeyCode.Name)
	if Input.KeyCode.Name == "A" and not Event then -- example the value of the string value is 'A'
		-- code here
	end
end)

well you should check the keycode name it returns so you know exactly what to put in your string value

2 Likes

I was talking about something like this

local Key = script.Key.Value
local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Key then
		--Do something
	end
end)

obviously that didn’t work seeing as how I came here, but thats an example of what I was asking. Sorry for not giving an example before.

I don’t know what you’re asking for here. You already have your solution, or am I understanding it wrong? Please elaborate.

1 Like

My last reply was the most I could elaborate, if you dont understand I will wait for someone else to respond.

No like, what are you basically trying to do in the code? Like change the value of something, tween etc?

it is the condition on the input was just an example
cant you just replace the string value on it?

also the keycode name is better to compare to a string
else you will write the whole characters of the enumaration on the string value

local Key = script.Key.Value -- the value of this is example "A"
local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode.Name == Key then
		--Do something
	end
end)

I answered that In my first message

I made one for my game i will share here :slight_smile:

local UserInputService = game:GetService("UserInputService")


UserInputService.InputBegan:Connect(function(inputObject, gameProcessedEvent)
	if not gameProcessedEvent then
		if inputObject.KeyCode == Enum.KeyCode.B then
			if script.Parent.AdminFrame.Visible == false then
				script.Parent.AdminFrame.Visible = true
			else
				script.Parent.AdminFrame.Visible = false
			end
			if game.StarterGui.PlayerGui.Welcome.Visible == true then
				script.Parent.AdminFrame.Visible = false
				game.ReplicatedStorage:WaitForChild("Sounds").ErrorSound:Play()
				game.StarterGui.PlayerGui.ErrorWelcome.Visible = true
				wait(2)
				game.StarterGui.PlayerGui.ErrorWelcome.Visible = false
			end
		end
	end
end)
2 Likes