How do I connect a MouseButton1Down to this function?

  1. What do you want to achieve?
    I want the player to either be able to press the Up Down Left Right button on their keyboard or the UpB DownB LeftB RightB ImageButton in the ScreenGUI for the function.

  2. What is the issue?
    When I click anywhere on the screen, it will still count as a “win”; however, I want it so they have to press the correct ImageButton (or use their keyboard).

  3. What solutions have you tried so far?
    I’ve tried rewriting the code a couple of times but I still have the same issue or it doesn’t work.

EDIT: This is a LocalScript within the ScreenGUI & I have no output errors

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local uis = game:GetService("UserInputService")
local arrow = game.ReplicatedStorage.Arrow:Clone()
local Main = script.Parent.Main
local runservice = game:GetService("RunService")
local song = game.ReplicatedStorage.Songs["Simulator Tune"]
local arrowSpeed = 2
local UpConnection
local DownConnection
local LeftConnection
local RightConnection
local cam = workspace.CurrentCamera
local finished = false

local LeftB = script.Parent.Main.LeftB
local RightB = script.Parent.Main.RightB
local UpB = script.Parent.Main.UpB
local DownB = script.Parent.Main.DownB

local arrowTypes = {
	"UP",
	"DOWN",
	"LEFT",
	"RIGHT",
}


function AddArrow(ArrowType)
	if ArrowType == "UP" then
		local animation = humanoid:LoadAnimation(game.ReplicatedStorage.Animations.Dance1)
		animation:Play()
		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)
		UpConnection = uis.InputBegan:Connect(function(input)
--here is where I try to connect the UpB button
			if input.KeyCode == Enum.KeyCode.Up or UpB.MouseButton1Click:Connect()
			then
				if (clonedArrow.AbsolutePosition - script.Parent.Main.Up.AbsolutePosition).Magnitude < 80 then
					game.ReplicatedStorage.SFX.Success:Play()
					script.Parent.Main.Up.ImageTransparency = 0
					wait(.2)
					script.Parent.Main.Up.ImageTransparency = .4
				end
			end
		end)
		wait(1)
		UpConnection:Disconnect()
		animation:Stop()
		clonedArrow:Destroy()
		return clonedArrow
	elseif ArrowType == "DOWN" then
		local animation = humanoid:LoadAnimation(game.ReplicatedStorage.Animations.Dance4)
		animation:Play()
			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)
		DownConnection = uis.InputBegan:Connect(function(input)
--here is where I try to connect the DownB button
			if input.KeyCode == Enum.KeyCode.Down or DownB.MouseButton1Click:Connect() 
			then
				if (clonedArrow.AbsolutePosition - script.Parent.Main.Down.AbsolutePosition).Magnitude < 80 then
					game.ReplicatedStorage.SFX.Success:Play()
					script.Parent.Main.Down.ImageTransparency = 0
					wait(.2)
					script.Parent.Main.Down.ImageTransparency = .4
				end
			end
		end)
		wait(1)
		DownConnection:Disconnect()
		animation:Stop()
		clonedArrow:Destroy()
		return clonedArrow

--There is more to the script for the other buttons; but I felt it was unneeded for this post

Thank you!

I’ve also tried adding this to my script (see below); however, it clones the arrow instead of doing what it’s supposed to.

UpB.MouseButton1Down:Connect(function()
	AddArrow("UP")
end)
DownB.MouseButton1Down:Connect(function()
	AddArrow("DOWN")
end)
LeftB.MouseButton1Down:Connect(function()
	AddArrow("LEFT")
end)
RightB.MouseButton1Down:Connect(function()
	AddArrow("RIGHT")
end)

well thats because your adding an arrow AddArrow("DIRECTION"), just rotate the already existing arrow to a specific rotation (no adding or subtracting) when its pressed

even on the script without it, it clones the arrow, just rotate the existing arrow

I’m not trying to rotate or add an arrow, I’m trying to do this:

It’s a game like FNF (Friday Night Funkin) where you have to press the GUIbutton (or the button on your keyboard) when the arrow is aligned with the arrow on the top of the screen.

get the key thats pressed (your already done that), and when said key gets pressed within a certain timeframe (call the key when the arrow is going over) count it as a hit, and if another button is pressed, count it as a miss (theres more complicated stuff like good, bad, but im not going into that detail)

I’ve already written the code for the key, I’m trying to connect the UpB.MouseButton1Down:Connect(function() to UpConnection, the DownB.MouseButton1Down:Connect(function() to DownConnection, the
LeftB.MouseButton1Down:Connect(function() to LeftConnection, and the
RightB.MouseButton1Down:Connect(function() to RightConnection

In my script, I have this written:

function AddArrow(ArrowType)
	if ArrowType == "UP" then
		local animation = humanoid:LoadAnimation(game.ReplicatedStorage.Animations.Dance1)
		animation:Play()
		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)
		UpConnection = uis.InputBegan:Connect(function(input)
			if input.KeyCode == Enum.KeyCode.Up
			then
				if (clonedArrow.AbsolutePosition - script.Parent.Main.Up.AbsolutePosition).Magnitude < 80 then
					game.ReplicatedStorage.SFX.Success:Play()
					script.Parent.Main.Up.ImageTransparency = 0
					wait(.2)
					script.Parent.Main.Up.ImageTransparency = .4
				end
			end
		end)
		wait(1)
		UpConnection:Disconnect()
		animation:Stop()
		clonedArrow:Destroy()
		return clonedArrow
	elseif ArrowType == "DOWN" then
--- the rest of the script

The keyboard key code works fine, here’s a video of the keyboard code: dance_minigame on Vimeo

The arrow is supposed to temporarily change transparency if the player is successful (as you can see in the video & code)

Overall, I’m trying to make my game accessible to mobile players

UpConnection = uis.InputBegan:Connect(function(input)
			if input.KeyCode == Enum.KeyCode.Up or (input.UserInputType == Enum.UserInputType.MouseButton1 and input.Name == "UpB") then
				if (clonedArrow.AbsolutePosition - script.Parent.Main.Up.AbsolutePosition).Magnitude < 80 then
					game.ReplicatedStorage.SFX.Success:Play()
					script.Parent.Main.Up.ImageTransparency = 0
					wait(.2)
					script.Parent.Main.Up.ImageTransparency = .4
				end
			end
		end)
		UpB.MouseButton1Click:Connect(function()
			if (clonedArrow.AbsolutePosition - script.Parent.Main.Up.AbsolutePosition).Magnitude < 80 then
				game.ReplicatedStorage.SFX.Success:Play()
				script.Parent.Main.Up.ImageTransparency = 0
				wait(.2)
				script.Parent.Main.Up.ImageTransparency = .4
			end
		end)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.