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

How do I check if the mouse is in within a textbutton without the mouseenter function?

function aa()

while wait() do

if script.Parent.TextButton.MouseEnter then

print("A")

end

end

end

  

aa()

I know theres a function called MouseEnter, but I need an if statement and not a function. How would I make it work?

1 comment

0

I have edited my answer! I hope it solves your problem.

2 answers

GuiObject.MouseEnter is not a property or a function.

It's an event.

You can listen for this event.

textButton.MouseEnter:Connect(function(x, y)
	print(string.format("Player clicked at coordinates: (%d, %d)", x, y));
end);

The x and y parameters are the 2D X-Y coordinates of the mouse when hovered over the button


Edit

I misunderstood your question the first time.

What you can do is have a variable, it will start off as false, set it to true when they are hovering over the button, false when they stop.


local isHovering = false;

textButton.MouseEnter:Connect(function(x, y)
	isHovering = true;
end);

textButton.MouseLeave:Connect(function(x, y)
	isHovering = false;
end);

If you need to check that they are hovering...

if (isHovering) then
	--// do something...
end

Side note

Do not use while wait() do. It only works because of a hack; a trick. Use

while true do
	...
end

It's clearer.

1 comment

0

Yes, I know. I stated that in the question. I'm looking for something that works like an if statement.

Hello, when I script UI's I like to be precise like you. Therefore you may use the following script to achieve the result:

pointer = game:GetService("UserInputService")
-- now we do the script, very easy
function response(theTextBox)
-- your code
end

if pointer.TextBoxFocused:Wait() then
-- I'm not sure if wait is a one-off thing...
pointer.TextBoxFocused:Connect(response)
end

-- the TextBoxFocusReleased is the reverse

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