Why does this print but not work

  1. What do you want to achieve?
    I’m trying to change text of textbox to the enum keycode if the player types one in
  2. What is the issue?
    the print works but the if statement wont, it also starts up with errors
local textui = script.Parent


textui.Changed:Connect(function()
	if textui.Text:upper() == Enum.KeyCode[textui.Text:upper()] then
		print(textui.Text:upper())
	end
	print(Enum.KeyCode[textui.Text:upper()])
end)

You’re trying to compare a string to an EnumItem.

If you’re trying to see whether or not the EnumItem exists, simply use just the key lookup:

if Enum.KeyCode[textui.Text:upper()] then
print(textui.Text:upper())
end

that works but how would I make it not start with errors?

What are the errors your having? I’m assuming it’s due to the direct key lookups:

textui.Changed:Connect(function()
	if Enum.KeyCode[textui.Text:upper()] then
		print(textui.Text:upper(), Enum.KeyCode[textui.Text:upper()]) 
    -- putting the 'print' in the if statement will prevent any errors
	end
end)

no it’s during start up even with the code in the if statement, I think it’s because textui.changed fires multiple times when a player joins.
the errors are a bunch of these
https://gyazo.com/50aad04fb3de47f3b2d9fef3fcecf362

local textui = script.Parent
local player = game.Players.LocalPlayer
player.PlayerGui.ScreenGui.Enabled = true

textui.Changed:Connect(function()
	if Enum.KeyCode[textui.Text:upper()] then
		textui.Text = Enum.KeyCode[textui.Text:upper()].Name
	end
	print(Enum.KeyCode[textui.Text:upper()])
end)

I see. You’d either have to put the other ‘print’ in the already existing if statement or set up the connection after a little bit. Preferably the latter, that which I gave an example of above.

that was just debug code because I was confused why that was printing but the if statement wasn’t. What the textbox ui changed prints on startup is information on itself you can see here https://gyazo.com/c0ef7fb7039937db26d1985707def2a9

I put the default text as “f” so it wouldnt start with errors, but if I remove the text to change it, it’ll still error, should I use pcall?

local textui = script.Parent
local player = game.Players.LocalPlayer
player.PlayerGui.ScreenGui.Enabled = true

textui.Changed:Connect(function(this)
	print(this)
	if Enum.KeyCode[textui.Text:upper()] then
		textui.Text = Enum.KeyCode[textui.Text:upper()].Name
	else
		print("cant")
		return
	end
end)

Try connecting the function to :GetPropertyChangedSignal(“Text”) rather than .Changed. This will only fire when the text changes, so it should only change via user input.

textui:GetPropertyChangedSignal("Text"):Connect(function()
	local upper = textui.Text:upper()
	if Enum.KeyCode[upper] then
		textui.Text = Enum.KeyCode[upper].Name
	else
		print("cant")
		return
	end
end)

I tried using focuslost and that works without errors, I was reading roblox api and they said it here

I’ll try using getpropertychanged too.

the getpropertychanged works but it’ll error as you’re typing or if you accidently type multiple things even in the if statement and “cant” wont print, the focuslost will do the same thing but if you press enter, should I use pcall to prevent errors?

local textui = script.Parent
local player = game.Players.LocalPlayer
player.PlayerGui.ScreenGui.Enabled = true

textui.FocusLost:Connect(function(enterpressed, input)
	if enterpressed then
		if Enum.KeyCode[textui.Text:upper()] then
			textui.Text = Enum.KeyCode[textui.Text:upper()].Name
		end
	end
end)

Before using pcall, which is unnecessary here (pcalls should be used if the code works but is inconsistent), try using FromName:

textui:GetPropertyChangedSignal("Text"):Connect(function()
	local upper = textui.Text:upper()
	local enumItem = Enum.KeyCode:FromName(upper)
	if enumItem then
		textui.Text = enumItem.Name
	else
		print("cant")
		return
	end
end)

This seems to work perfectly on my end.

Thank you, that works. God bless.

1 Like