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!