Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago
Edited 5 years ago

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

Log in to vote
1
Answered by
chomboghai 2044 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

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)
0
Im rlly happy thank you so so so much! 3dwardIsBack -10 — 5y
0
No problem! If it helped out, make sure to mark the answer as correct so we all know you have a working solution. chomboghai 2044 — 5y
0
Oh ye and one more thing 3dwardIsBack -10 — 5y
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 3dwardIsBack -10 — 5y
View all comments (11 more)
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 chomboghai 2044 — 5y
0
To push the player back, check out the bodymovers: https://wiki.roblox.com/index.php?title=API:Class/BodyMover chomboghai 2044 — 5y
0
yay Now the target also sits now I will do the bodymove part now 3dwardIsBack -10 — 5y
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. chomboghai 2044 — 5y
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 3dwardIsBack -10 — 5y
0
Don't forget to select 'Accept answer' so other users know this question has been answered! chomboghai 2044 — 5y
0
ye I do it directly when Im ready 3dwardIsBack -10 — 5y
0
Can you post on this thread with the code formatted please? chomboghai 2044 — 5y
0
the code that I have right now? 3dwardIsBack -10 — 5y
0
I tried to understand the WIKI but I don't get it 3dwardIsBack -10 — 5y
0
So now I got it so far but its pretty bad it looks liek the target gets teleported not even pushed 3dwardIsBack -10 — 5y
Ad

Answer this question