How to use mousedown for a mobile button?

I have a key bind script that hurts a target when you press the k “H” but your mouse has to be on them. I’m trying replicate that into a button script for mobile and what I’m trying to achieve is when you press the button you have to click the target for the key bind to actually activate. I’m struggling though and would like some help!

local Plr = game.Players.LocalPlayer
local Char = Plr.Character or Plr.CharacterAdded:Wait()
local Hum = Char:WaitForChild("Humanoid")
local PainButton = script.Parent.PainInfliction
local ReplicatedS = game:GetService("ReplicatedStorage")
local PainEvent = ReplicatedS:WaitForChild("paininfliction")
local Debounce = true 
local connect 
local mouse = Plr:GetMouse()




local function RunHandler(input)
		if input and Char.Magic.Value >= 35 then
		connect = mouse.Button1Down:Connect(function()
			if mouse.Target.Parent:FindFirstChild("HumanoidRootPart") then
				connect:Disconnect()
				if (Plr.Character.HumanoidRootPart.Position - mouse.Hit.Position).Magnitude < 70 then
				PainEvent:FireServer(mouse.Target)
				wait(0.1)
				script.Parent.PainButton.Disabled = true
				wait(15)
					script.Parent.PainButton.Disabled = false
				else
					
				end
			end
		end)

PainButton.MouseButton1Down:Connect(function()
			RunHandler(true)
		end
end)

UserInputType.Touch since its mobile (ig?)

You can use TouchTap event for buttons. Trust me, it feels weird to hold a button to sprint in mobile.

But what I’m trying to do is when you tap the button and tap a person the script fires the event + I already have a running script

where’d you get sprint from lol

I’ll show you an example of what im trying to do

@x1_EE (Oh god, I don’t know where I’d read sprint)

You should do something like this:

local Mouse = Player:GetMouse()
local Button = script.Parent
local UIS = game:GetService("UserInputService")
-- 
Delay = 5
currentTarget = nil
--
UIS.InputEnded:Connect(function(input, gameProcessedEvent)
    if Mouse.Target.Parent:FindFirstChild("Humanoid") then
        currentTarget = Mouse.Target.Parent
        task.wait(Delay)
        currentTarget = nil
    end
    
end)

Button.TouchTap:Connect(function()
    if currentTarget then

        end
end)

hello i think mouse enter can help you


PainButton.MouseEnter:Connect(function()
			RunHandler(true)
		end
end)

15:35:13.120 Players.Funnyskyswagway3.PlayerGui.DarkJoMobile.PainButton:15: attempt to index nil with ‘Parent’ - Client - PainButton:15

This is what I get.

Local script in StarterCharacterScripts.

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local Button = Player.PlayerGui.ScreenGui.TextButton -- Replace with your button location
local UIS = game:GetService("UserInputService")
-- 
local Delay = 5
currentTarget = nil
--
UIS.InputEnded:Connect(function(input, gameProcessedEvent)
	if input.UserInputType == Enum.UserInputType.Touch then
		if Mouse.Target then
			if Mouse.Target.Parent:FindFirstChild("Humanoid") then
				currentTarget = Mouse.Target.Parent
				task.wait(Delay)
				currentTarget = nil
			end
		end
		
	end

end)

Button.TouchTap:Connect(function()
	if currentTarget then
		print(currentTarget)
	end
end)

Alternate: (Gets last character touched before button triggered)

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local Button = Player.PlayerGui.ScreenGui.TextButton -- Replace with your button location
local UIS = game:GetService("UserInputService")
-- 
local Delay = 5
currentTarget = nil
buttonTriggered = false
--
UIS.InputEnded:Connect(function(input, gameProcessedEvent)
	if input.UserInputType == Enum.UserInputType.Touch and not gameProcessedEvent and buttonTriggered then
		if Mouse.Target then
			if Mouse.Target.Parent:FindFirstChild("Humanoid") then
				currentTarget = Mouse.Target.Parent
			end
		end
		
	end

end)

Button.TouchTap:Connect(function()
	
	buttonTriggered = true
	
	task.wait(Delay)
	
	if currentTarget then
		print(currentTarget)
		
		currentTarget = nil
		
	end
	
	buttonTriggered = false
	
end)

For a general example this is what im trying to achieve. When you say the spell and click someone it activates, im trying to do this with a mobile button.

¿You want to check the last player touched before touching a button? ¿Or after?

1 Like

When you touch the button then click the player after it’s been pressed

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local Button = Player.PlayerGui.ScreenGui.TextButton -- Replace with your button location
local UIS = game:GetService("UserInputService")
-- 
local Delay = 5
currentTarget = nil
buttonTriggered = false
--
UIS.InputEnded:Connect(function(input, gameProcessedEvent)
	if input.UserInputType == Enum.UserInputType.Touch and not gameProcessedEvent and buttonTriggered then
		if Mouse.Target then
			if Mouse.Target.Parent:FindFirstChild("Humanoid") then
				currentTarget = Mouse.Target.Parent
			end
		end
		
	end

end)

Button.TouchTap:Connect(function()
	
	buttonTriggered = true
	
	task.wait(Delay)
	
	if currentTarget then
		print(currentTarget)
		
		currentTarget = nil
		
	end
	
	buttonTriggered = false
	
end)

YES! Thank you so much this is exactly what I needed.

1 Like