I want the target to be hit when the mouse is over it and x is pressed. I'm new to this part any help is greatly appreciated.
local plr = game.Players.LocalPlayer; local mouse = plr:GetMouse(); local torso = plr.Character.Humanoid.Torso function onKeyPress(actionName, userInputState, inputObject) if userInputState == Enum.UserInputState.Begin then torso.Velocity = torso.CFrame.lookVector*-63 plr.Character.Humanoid.Health = plr.Character.Humanoid.Health - 25 plr.Character.Humanoid.Sit = true wait(5) plr.Character.Humanoid.Sit = false end end game.ContextActionService:BindAction("keyPress", onKeyPress, false, Enum.KeyCode.X) mouse.Move:connect(function() local X, Y = mouse.X, mouse.Y; if mouse.Target and plr.Target.Humanoid.Health == - 25 and plr.Target.Humanoid.Sit then if game.Players:GetPlayerFromCharacter(mouse.Target.Parent) and X > script.Parent.AbsolutePosition.x and Y > script.Parent.AbsolutePosition.y and X < script.Parent.AbsolutePosition.x+script.Parent.AbsoluteSize.x and Y < script.Parent.AbsolutePosition.y+script.Parent.AbsoluteSize.y then else end else end end)
Let's look at what you're asking here.
You say you want the target to be hit when:
The mouse is over it
X is pressed
Let's check for these two conditions. It's easier to start the check from a key press using ContextActionService
.
local ContextActionService = game:GetService("ContextActionService") local function pressedXHandler() -- feel free to change the function name to anything you want -- this function will fire when X is pressed end ContextActionService:BindAction("nameOfAction", pressedXHandler, false, Enum.KeyCode.X) -- change "nameOfAction" to whatever action name you want
Now we have bound the X key to pressedXHandler
. Now, we should check inside the handler if the mouse is hovering over the target. We can use the :GetMouse()
method, like you already have, to get the mouse of the player.
I'm assuming the 'target' you are looking for is other players - so I will make the target look for a character object. If that's not the case, feel free to adjust the script and change the target.
local ContextActionService = game:GetService("ContextActionService") -- need to reference the mouse local player = game.Players.LocalPlayer local mouse = player:GetMouse() local function pressedXHandler() local hitCharacter = mouse.Target.Parent local hitHumanoid = hitCharacter:FindFirstChildWhichIsA("Humanoid") if hitHumanoid then -- The mouse was hovering over another player, perform action -- Assuming you want the hit character to take 25 damage: hitHumanoid:TakeDamage(25) end end ContextActionService:BindAction("nameOfAction", pressedXHandler, false, Enum.KeyCode.X)
This script should be sufficient. I hope it helps :)
EDIT: Adding Cooldowns (Debounce)
Add a variable in your script that only allows the pressedXHandler
function to run when the variable is set to true
or false
(usually depending on the name of the variable)
Here's what I mean:
local ContextActionService = game:GetService("ContextActionService") local player = game.Players.LocalPlayer local mouse = player:GetMouse() -- Add a debounce variable local debounce = false local function pressedXHandler() if debounce then return end -- This will stop the function from running if debounce is true debounce = true -- Set debounce to true so the function can't be called again local hitCharacter = mouse.Target.Parent local hitHumanoid = hitCharacter:FindFirstChildWhichIsA("Humanoid") if hitHumanoid then -- The mouse was hovering over another player, perform action -- Assuming you want the hit character to take 25 damage: hitHumanoid:TakeDamage(25) end wait(1) -- This is the cooldown time debounce = false -- Make sure to set debounce back to false in the end end ContextActionService:BindAction("nameOfAction", pressedXHandler, false, Enum.KeyCode.X)