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

How to get a reference to the GUI element that was interacted with?

I have a frame which has a left and a right frame within (or image/button.)

I'm trying to connect the children frames to a function which in turn needs to reference the frame called the function. The input parameter only provides information about the delta, keycode, position, user input state and type.

Is there any way to find out which frame was clicked? (Other than having to compute it using the coordinates of each frame?)

local parentFrame = game.Players.LocalPlayer.PlayerGui.ScreenGui.Frame
local leftFrame = parentFrame.LeftFrame
local rightFrame = parentFrame.RightFrame

local function foo(input)
	--how can I get the frame that was interacted with in here?
	--local clickedFrame = ???
	
	--code goes here...
end

leftFrame.InputBegan:Connect(foo)
rightFrame.InputBegan:Connect(foo)

1 answer

✓ Accepted answer
local parentFrame = game.Players.LocalPlayer.PlayerGui.ScreenGui.Frame
local leftFrame = parentFrame.LeftFrame
local rightFrame = parentFrame.RightFrame

local function foo(input, frame)
    --how can I get the frame that was interacted with in here?
    --local clickedFrame = frame

    --code goes here...
end

leftFrame.InputBegan:Connect(function(input)
	foo(input, leftFrame)
	-- or just do foo code here idk
end)
rightFrame.InputBegan:Connect(function(input)
	foo(input, rightFrame)	
	-- or just do foo code here idk
end)

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