Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How can I finish this ? I can't figure out how I can make the push with Bodymovers

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)

1 answer

Let's look at what you're asking here.

You say you want the target to be hit when:

  1. The mouse is over it

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

15 comments

0

Im rlly happy thank you so so so much!

0

No problem! If it helped out, make sure to mark the answer as correct so we all know you have a working solution.

0

Oh ye and one more thing

0

I want the target to be pushed and also sit when pushed but can I just rename it then to target?And also putting cooldown between one push ( - 25 damage and the pushback

0

To make the person sit down, you can use the change state function on Humanoid: http://wiki.roblox.com/index.php?title=API:Class/Humanoid/ChangeState

0

To push the player back, check out the bodymovers: https://wiki.roblox.com/index.php?title=API:Class/BodyMover

0

yay Now the target also sits now I will do the bodymove part now

0

Good job! I'm glad you were able to figure that part out. And yes, I didn't add the sitting or the pushing to my script, it was just a layout for checking if the mouse is on the target and the user is pressing X.

0

Yes I already had a great start with your help I now really understand it I'm now making it so that the target not only gets pushed and sits but also makes a circle

0

Don't forget to select 'Accept answer' so other users know this question has been answered!

0

ye I do it directly when Im ready

0

Can you post on this thread with the code formatted please?

0

the code that I have right now?

0

I tried to understand the WIKI but I don't get it

0

So now I got it so far but its pretty bad it looks liek the target gets teleported not even pushed

This discussion is preserved as it was. New answers, comments, and votes are closed.