ImageButton surface gui not fire mouse event

So, what I want to achieve is to make a button on the workspace which the player can press on it.

This is what I have in a local script located in StarterPlayerScripts

local player = game.Players.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")
local folder = playerGui:WaitForChild("RefreshGlobalLeaderboard")
local donateButton = folder:WaitForChild("DonateButton"):WaitForChild("ImageButton")

-- I clicked on that button, the "HELLO" text never print out
donateButton.MouseButton1Up:Connect(function()
	print("HELLO")
end)

This is where the UI instance be
Screen Shot 2022-01-06 at 1.25.43 pm

This is where it located on workspace
Screen Shot 2022-01-06 at 1.31.25 pm

The DonateButton surface gui properties


You can see that the adornee have set to object in workspace and the Active has tick on. (sorry it has the same name it might cause confusion but I really set it to the DonateButton on workspace, still it’s not work)

The only twist is that when I changed the event to

-- I move my mouse hover the button and it print "HELLO"
donateButton.MouseEnter:Connect(function()
	print("HELLO")
end)

It’s work, and that’s made me more frustrating, please if anyone knows what the problem is.

Edit: This event does not work anymore after player reset their character even I turned off the ResetOnSpawn on surface gui which is kinda weird

1 Like

I try using your code it’s working perfectly fine for me are you sure nothing blocking the surface gui? (Transparency part include) for it’s not working when respawn try restart script when character respawn (not sure why this happened)

(Not sure if streamable died again)
Here code i used:

local player = game.Players.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")
local donateButton = playerGui:WaitForChild("DonateButton").ImageButton --Surface Gui in PlayerGui


donateButton.MouseButton1Up:Connect(function()
	print("Mouse 1 Up")
end)

donateButton.MouseEnter:Connect(function()
	print("Mouse Entered")
end)


player.CharacterAdded:Connect(function()
	script.Disabled = true
	task.wait()
	script.Disabled = false
end)
2 Likes

Wow, thank you for your reply. This code solves the respawn problem perfectly (but idk why it works as well might be the Roblox problem maybe?)

However, the main problem remains the same. There’s no other transparency object blocking the way. I’m not sure is it because I bind the ContextActionService:BindAction() with the Enum.UserInputType.MouseButton1? I really wish it’s not cuz that bind action is really important for my game. But if it’s because of that I might need to think of a better way to display the button on workspace. If you happen to know a better solution please recommend me, anyway thanks for your testing.

Oh you bind Mouse1 for other function?

Yeah, I not sure is it the cause of this problem? The other UI from screen gui is working fine except this surface gui so I not entirely sure that is the problem.

If it’s not too much what is the ContextActionService Action name that you bind to MouseButton1?

ContextActionService:BindAction("BasicAttack", BasicAttack, false, Enum.UserInputType.MouseButton1)

This is what it looks like. (The second param is local function inside the same script)

Nvm that I just found new solution here the code

local function onDonateButtonClicked(actionName, inputState)
	
	if inputState == Enum.UserInputState.End then --End = mouse up
		print("Mouse 1 Up for donate")
	end
	
end


donateButton.MouseEnter:Connect(function()
	print("Mouse Entered")
	--Temp bind mouse event
	ContextActionService:BindAction("Mouse1Donate", onDonateButtonClicked, true, Enum.UserInputType.MouseButton1)
end)

donateButton.MouseLeave:Connect(function()
	--unbind temp mouse event
	ContextActionService:UnbindAction("Mouse1Donate")
end)

We bind “Mouse1Donate” when mouse is enter the surface gui in unbind it when leave surface gui

[important note]
the action name you bind for “Mouse1Donate” shouldn’t same with other action name

tell me if it work

1 Like

Yes, it’s work now thanks so much! but I still don’t get why the MouseButton1Up is not working lol. Guess I’m still new to this kind of stuff.

The ContextActionService is blocking it i guess anyway good luck making your experience

1 Like