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)
@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)
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.
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)