Check if a player clicked out of a image label

hey, devs! I’m currently wondering how I can check if a player clicked out of an image label. any help would be appreciated, thank you!

1 Like

One approach would be to declare a variable that changes to true on MouseEnter and false on MouseLeave, and once you need to check if it’s outside of the ImgaeLabel, just check if the value of the variable is false. Another approach would be to calculate if the mouse’s position is outside of the boundaries of the ImageLabel (This approach will not really be more efficient than the other approach, but just in case you would not want to deal with events).

I want the player to click the outside, and not just hover over the outside is the thing.

Then shouldn’t the suggested approach work?

If the mouse leave the image label in the first approach, wouldn’t the “clicking out” thing trigger?

Oh I meant that you will just rely on that variable to decide whether or not the mouse is within the boundaries of the image. You will use UserInputService to detect mouse inputs.

Ahh, gotcha. Thank you. I was confused at first.

1 Like

Not sure why this isn’t working

local UIS = game:GetService("UserInputService")

local mouseStat = false -- true = out; false; in

script.Parent.MouseEnter:Connect(function()
	mouseStat = false
	print(mouseStat)
end)

script.Parent.MouseLeave:Connect(function()
	mouseStat = true
	print(mouseStat)
end)

UIS.InputBegan:Connect(function(input, gameProcessed)
	if input.UserInputType == Enum.UserInputType.MouseButton1 and mouseStat == true then
		game.Players.LocalPlayer.PlayerGui.OrderingUI.ImageLabel:TweenPosition(UDim2.new(0.5, 0, 0.5, 0))
		game.Players.LocalPlayer.PlayerGui.OrderingUI.Receipt:TweenPosition(UDim2.new(0.913, 0, 0.5, 0))
	end
	
	if gameProcessed then
		print("\tThe game engine internally observed this input!")
	else
		print("\tThe game engine did not internally observe this input!")
	end
end)

It’s saying that the game couldn’t figure out the input

Can you print the input? It should definitely detect Mouse Inputs.

Is the issue that the game processed event is false?

No, as I did a print(mouse stat) whenever the mouse leave to enters.

From what did you infer that the game couldn’t figure out the input?

I’m not too sure.It could be something inside the script. Do you mind pasting the script and testing it?

I do not really understand what the issue is, which part of the code code doesn’t run correctly?
Try printing out Input.UserInputType.

I think I’m getting what’s wrong now. Whenever I click on the ui, the game observes it, however, when I click out, it doesn’t

Then just check whether mouseStat equals false.

if input.UserInputType == Enum.UserInputType.MouseButton1 and mouseStat == true then
1 Like

Why not just do,

if gameProcessed then
	return
end

Edit: Here, try this:

UIS.InputBegan:Connect(function(input, gameProcessed)
	if gameProcessed then
		return
	end
	if input.UserInputType == Enum.UserInputType.MouseButton1 and gameProcessed == false then
		game.Players.LocalPlayer.PlayerGui.OrderingUI.ImageLabel:TweenPosition(UDim2.new(0.5, 0, 0.5, 0))
		game.Players.LocalPlayer.PlayerGui.OrderingUI.Receipt:TweenPosition(UDim2.new(0.913, 0, 0.5, 0))
	end
end)
1 Like