This is probably a stupid question to ask, but I couldn’t find anything helpful on the forum. It’s in the title. How do you detect mouse clicks on viewportframes?
Would it be an option to create an invisible TextButton overlay on it, then use the Activated method on that??
Otherwise, I’m not entirely sure what events are available with ViewportFrames.
Check when the player’s mouse enters the viewport frame, then check when the mouse is clicked
So how would I do that? With UserInputService?
Cant write any code or puesdo code at the moment but check this out:GuiObject | Documentation - Roblox Creator Hub
And this:
GuiButton | Documentation - Roblox Creator Hub
If you need any help let me know!
I don’t really know how to do it, can you help me or give an example?
Sure!, it might not be perfect but its just an example
local Viewportframe = script.Parent ----ViewportFrame
local Mouse = game.Players.LocalPlayer:GetMouse()
Viewportframe.MouseEnter:Connect(function()--------Fires when player's mouse enters the viewport frame
local MouseFunc
MouseFunc = Mouse.Button1Down:Connect(function() -----fires when player clicks
----------Do what ever here
print("ViewPortFrameClicked")
end)
Viewportframe.MouseLeave:Connect(function() -----Fires when player's mouse leaves the viewport frame
if MouseFunc then
MouseFunc:Disconnect()-----Disconnects Mouse.Button1Down function(to ensure the player can only click once they have entered the viewportframe)
end
end)
end)
Simpler/Additional Method:
A cleaner method than unnecessarily connecting and disconnecting functions or fetching the player’s mouse is to use InputBegan which it inherits from an abstract superclass, GuiObject. You can use this similarly to UserInputService’s InputBegan. Some benefits are that it only acts on the GuiObject itself and it automatically respects gameProcessedEvent (you do not need to handle it unlike UIS).
local viewportFrame = something
viewportFrame.InputBegan:Connect(function (inputObject)
if inputObject.UserInputType == Enum.UserInputType.MouseMovement then
-- Mouse entered
elseif inputObject.UserInputType == Enum.UserInputType.MouseButton1 then
-- Mouse clicked
end
end)
viewportFrame.InputEnded:Connect(function (inputObject)
if inputObject.UserInputType == Enum.UserInputType.MouseMovement then
-- Mouse left
end
end)
I totally Forgot about using input began or input ended , your method is way cleaner, thanks for reminding/ letting me know! (i’ll quote that in my previous post)