How do I check if the player pressed arrowkeys and the position of a UI is the position of another UI?

Hello, recently I have been working on a game like FNF and I am struggling with checking if the player pressed arrowkeys and the position of a UI is the position of another UI and how can I implement it to this script and then stop the event when the UI is destroyed?

local player = game.Players.LocalPlayer
local uis = game:GetService('UserInputService')
local arrow = game.ReplicatedStorage.Arrow
local Main = script.Parent.Main
local song = game.ReplicatedStorage.Songs["Chasing Lights"]
local arrowSpeed = 2


local arrowTypes = {
    'UP',
    'Down',
    'Left',
    'Right'
}

function AddArrow(ArrowType)
    if ArrowType == 'UP' then
        local clonedArrow = arrow:Clone()
        clonedArrow.Parent = Main
        local posX = script.Parent.Main.Up.Position.X.Scale
        local posY = script.Parent.Main.Up.Position.Y.Scale

        clonedArrow.Position = UDim2.new(posX,0,1,0)
        clonedArrow.ImageColor3 = script.Parent.Main.Up.ImageColor3
        clonedArrow.Rotation = 180
		clonedArrow:TweenPosition(UDim2.new(posX,0,-1,0), nil, nil, arrowSpeed)
		uis.InputBegan:Connect(function(input)
			if input.KeyCode == Enum.KeyCode.Up  then
				print('yes!')
			end
		end)
		wait(1)
        clonedArrow:Destroy()
        return clonedArrow
    elseif ArrowType == 'Down' then
        local clonedArrow = arrow:Clone()
        clonedArrow.Parent = Main
        local posX = script.Parent.Main.Down.Position.X.Scale
        local posY = script.Parent.Main.Down.Position.Y.Scale

        clonedArrow.Position = UDim2.new(posX,0,1,0)
        clonedArrow.ImageColor3 = script.Parent.Main.Down.ImageColor3
        clonedArrow.Rotation = 0
        clonedArrow:TweenPosition(UDim2.new(posX,0,-1,0), nil, nil ,arrowSpeed)
        wait(1)
        clonedArrow:Destroy()
        return clonedArrow
    elseif ArrowType == 'Left' then
        local clonedArrow = arrow:Clone()
        clonedArrow.Parent = Main
        local posX = script.Parent.Main.Left.Position.X.Scale
        local posY = script.Parent.Main.Left.Position.Y.Scale

        clonedArrow.Position = UDim2.new(posX,0,1,0)
        clonedArrow.ImageColor3 = script.Parent.Main.Left.ImageColor3
        clonedArrow.Rotation = 90
        clonedArrow:TweenPosition(UDim2.new(posX,0,-1,0),nil,nil,arrowSpeed)
        wait(1)
        clonedArrow:Destroy()
        return clonedArrow
    elseif ArrowType == 'Right' then
        local clonedArrow = arrow:Clone()
        clonedArrow.Parent = Main
        local posX = script.Parent.Main.Right.Position.X.Scale
        local posY = script.Parent.Main.Right.Position.Y.Scale
        clonedArrow.Position = UDim2.new(posX,0,1,0)
        clonedArrow.ImageColor3 = script.Parent.Main.Right.ImageColor3
        clonedArrow.Rotation = -90
        clonedArrow:TweenPosition(UDim2.new(posX,0,-1,0),nil,nil,arrowSpeed)
        wait(1)
        clonedArrow:Destroy()
        return clonedArrow
    end
end

while wait(1) do
    local number = math.round(song.PlaybackLoudness/35)
    local chosenArrow = math.clamp(number,1,#arrowTypes)
    coroutine.wrap(AddArrow)(arrowTypes[chosenArrow])    
end

I was trying to use connections but It didn’t really work.
Thank you!

You can use user input service to detect key presses

local uis = game:GetService("UserInputService")

uis.InputBegan:Connect(function(input)
    if input.KeyCode = Enum.KeyCode.Left then
       print("Left arrow pressed")
   end
end)
3 Likes

I know and It’s already in the script but how do I check if the cloned arrow has the same position of the main arrow.

I would recommend using magnitude so the ui can also be a bit off. If you want it to be exactly the same position you can use

if ui.Position == ui2.Position then
    --code here
end
1 Like

And If I want to use magnitude I do this?

if ui.Position == ui2.Position or (ui.AbsolutePosition - ui2.AbsolutePosition).magnitude < 5 then
    --code here
end

Yes, that will work I think. (adding some characters so I can send)

1 Like

I tried it It’s sadly not working.