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

[SOLVED] Check TextBox value when pressing enter only?

Hey guys,

I've been making an admin panel, and it uses a TextBox to enter the Target's name. The idea is that upon changing the TextBox.Text, the script checks if the entered player exists, and returns a print("Found!").

However, while typing the name, the script just keeps on printing "Nope!" every letter you type. How do I make it check only when the user presses Enter when in the TextBox selection, so it doesn't spam print"Nope!" anymore?

local target = script.Parent.Text

script.Parent.Changed:Connect(function()
	if game.Players:FindFirstChild(target) then
		print("Found!")
	else
		print("Nope!")
	end
end)

1 answer

✓ Accepted answer

###There is an event of the TextBox called FocusLost, which runs when you lose focus of the TextBox, whether it be you clicked away from it or you pressed enter. Lucky for you, the parameter to FocusLost is a boolean that is true if the reason for losing focus is because of pressing enter.

local Players = game:GetService("Players")
local player = Players.LocalPlayer 
local target = script.Parent -- This variable won't update if the text changes, make a direct reference to the text property instead

script.Parent.FocusLost:Connect(function(enterpressed)
    if enterpressed then
        if Players:FindFirstChild(target.Text) then
            if not Players[target.Text]:IsA("Player") then return end -- if it's not a player stop the function
            print("Found!")
        else
            print("Nope!")
        end
    end
end)

1 comment

0

Found a diffrent solution, but this too works! Thanks for the explanation, will be using this due to a better debug :)

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