Connecting event callback functions to GUI Objects/Frames in for loop

  1. What do you want to achieve?
    Implement game logic on multiple GUI objects (preferably at once). Basically, I’m trying to rotate some Frame objects based on mouse input. I can do that for each individual Frame object just fine, but the point is to use the angle that of the rotation of the individual Frame objects for game logic.

  2. What is the issue?
    My initial thought was to get all of the “draggable” Frames in a for loop and then connect callback functions to the InputBegan and InputEnded events. I suspected that there would be a problem and sure enough there was. Whenever you do some input event (in this case clicking and dragging), it applies the callback function to all of the Frames.

  3. What solutions have you tried so far? Did you look for solutions on the Creator Hub?
    The only other solution I could have is to manually add these event callback functions to each Frame, but that would be tedious as I plan to vary the number of Frames later.

local UserInputService = game:GetService("UserInputService")
local gui = script.Parent
local dragging
local dragInput
local dragStart 
local startPos

local targets = {} 
local movers = {} -- the draggable Frames

for _, child in gui:GetChildren() do
	if string.find(child.Name, "Mover") then table.insert(movers, child) end
	if string.find(child.Name, "Target") then table.insert(targets, child) end
end

local function getAngle(mouse, frameCenter)
	return (math.atan2(mouse.Y - frameCenter.Y, mouse.X - frameCenter.X))
end

local function radToDeg(a)
	return (a * 180 / math.pi)
end

local function getRotation(frame, mouse)
	local middleFrame = frame.AbsolutePosition + (frame.AbsoluteSize / 2)
	local angle = getAngle(mouse, middleFrame)
	local degrees = radToDeg(angle)
	return degrees
end

local function update(input, mover)
	local angle = getRotation(mover, Vector2.new(input.Position.X, input.Position.Y))
	mover.Rotation = angle + 90
end

for _, mover in movers do
	mover.InputBegan:Connect(function(input)
		if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch then
			dragging = true
			dragStart = input.Position
			startPos = mover.Position

			input.Changed:Connect(function()
				if input.UserInputState == Enum.UserInputState.End then
					dragging = false
				end
			end)
		end
	end)

	mover.InputChanged:Connect(function(input)
		if input.UserInputType == Enum.UserInputType.MouseMovement or input.UserInputType == Enum.UserInputType.Touch then
			dragInput = input
		end
	end)

	UserInputService.InputChanged:Connect(function(input)
		if input == dragInput and dragging then
			update(input, mover)
		end
	end)
end

for _, target in targets do

end

I’m not sure I completely understand, aren’t you trying to connect all the frames to the callback function so that they rotate with the mouse?

Sorry, I’ll try to clarify.

I want to rotate individual frame objects with the mouse, but using the code I provided, they all jump to one frame and then rotate along with it.

In the video, instead of the green frame rotating by itself, all frames move to the blue frame. I think I know why it does that, but I’m trying to find an alternative that allows me to move each frame individually within the same script.

Ah, i see.
I think you need to user UserInputService:InputBegan() and check for a mouseButton1 input.
Then use ScreenGuiName:GetGuiObjectsAtPosition() with the mouse location and check if any of the guiObjects returned are draggable. Then you can store the current gui that the player selected in a variable(e.g: CurrentGui).
Next, InputChanged function and check if there is a value in the CurrentGui variable and if so, apply the update function to it.
InputEnded function and check if mousebutton1 released and if so, set the CurrentGui variable to nil
Hope this helped and let me know if my explanation needs clarifying

Here is some sample code I typed up in 5 minutes to explain my point, I didn’t check syntax too carefully as I didn’t type this code in studio and the formatting is quite poor but this should explain what I mean

local UserInputService = game:GetService("UserInputService")
local gui = script.Parent
local dragging
local dragInput
local dragStart 
local startPos
local CurrentSelected

local targets = {} 
local movers = {} -- the draggable Frames

for _, child in gui:GetChildren() do
	if string.find(child.Name, "Mover") then table.insert(movers, child) end
	if string.find(child.Name, "Target") then table.insert(targets, child) end
end

local function getAngle(mouse, frameCenter)
	return (math.atan2(mouse.Y - frameCenter.Y, mouse.X - frameCenter.X))
end

local function radToDeg(a)
	return (a * 180 / math.pi)
end

local function getRotation(frame, mouse)
	local middleFrame = frame.AbsolutePosition + (frame.AbsoluteSize / 2)
	local angle = getAngle(mouse, middleFrame)
	local degrees = radToDeg(angle)
	return degrees
end

local function update(input, mover)
	local angle = getRotation(mover, Vector2.new(input.Position.X, input.Position.Y))
	mover.Rotation = angle + 90
end

UserInputService.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
local mousePos = UserInputService:GetMouse()
 local ObjectsUnderMouse = gui:GetGuiObjectsAtPosition(mousePos.X,mousePos.Y)
for x,v in pairs(ObjectsUnderMouse) do
if table.find(movers,v)  then
CurrentSelected = v
end
end
end
end)

UserInputService.InputChanged:Connect(function(input)
		if input == dragInput and CurrentSelected then
			update(input, CurrentSelected)
		end
	end)

UserInputService.InputEnded:Connect(function(input)
		if input.UserInputType == Enum.UserInputType.MouseButton1 then
			CurrentSelected = nil
		end
	end)

Thank you! This worked as I was hoping it would.

I have a few ideas on how this can be improved, but I’m just prototyping this idea so I’m not entirely worried about structure just yet.

UserInputService.InputBegan:Connect(function(input: InputObject, gameProcessedEvent: boolean) 
	if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch then
		local objsUnderMouse = LocalPlayer.PlayerGui:GetGuiObjectsAtPosition(input.Position.X, input.Position.Y)
		for _, obj in pairs(objsUnderMouse) do
			if table.find(movers, obj) then
				currentMover = obj.Parent
			end
		end
	end
	
	UserInputService.InputEnded:Connect(function(input: InputObject, gameProcessedEvent: boolean) 
		currentMover = nil
	end)
end)

UserInputService.InputChanged:Connect(function(input: InputObject, gameProcessedEvent: boolean) 
	if currentMover and input.UserInputType == Enum.UserInputType.MouseMovement or input.UserInputType == Enum.UserInputType.Touch then
		update(input, currentMover)
	end
end)