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 6 years ago
Edited 6 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.

01local plr = game.Players.LocalPlayer;
02local mouse = plr:GetMouse();
03local torso = plr.Character.Humanoid.Torso
04 
05function onKeyPress(actionName, userInputState, inputObject)
06    if userInputState == Enum.UserInputState.Begin then
07        torso.Velocity = torso.CFrame.lookVector*-63
08        plr.Character.Humanoid.Health = plr.Character.Humanoid.Health - 25
09        plr.Character.Humanoid.Sit = true
10        wait(5)
11        plr.Character.Humanoid.Sit = false
12    end
13end
14 
15game.ContextActionService:BindAction("keyPress", onKeyPress, false, Enum.KeyCode.X)
View all 33 lines...

1 answer

Log in to vote
1
Answered by
chomboghai 2044 Moderation Voter Community Moderator
6 years ago
Edited 6 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.

1local ContextActionService = game:GetService("ContextActionService")
2 
3local function pressedXHandler() -- feel free to change the function name to anything you want
4    -- this function will fire when X is pressed
5end
6 
7ContextActionService:BindAction("nameOfAction", pressedXHandler, false, Enum.KeyCode.X)
8-- 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.

01local ContextActionService = game:GetService("ContextActionService")
02 
03-- need to reference the mouse
04local player = game.Players.LocalPlayer
05local mouse = player:GetMouse()
06 
07local function pressedXHandler()
08    local hitCharacter = mouse.Target.Parent
09    local hitHumanoid = hitCharacter:FindFirstChildWhichIsA("Humanoid")
10    if hitHumanoid then
11        -- The mouse was hovering over another player, perform action
12        -- Assuming you want the hit character to take 25 damage:
13        hitHumanoid:TakeDamage(25)
14    end
15end
16 
17ContextActionService: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:

01local ContextActionService = game:GetService("ContextActionService")
02 
03local player = game.Players.LocalPlayer
04local mouse = player:GetMouse()
05 
06-- Add a debounce variable
07local debounce = false
08 
09local function pressedXHandler()
10    if debounce then return end -- This will stop the function from running if debounce is true
11    debounce = true -- Set debounce to true so the function can't be called again
12    local hitCharacter = mouse.Target.Parent
13    local hitHumanoid = hitCharacter:FindFirstChildWhichIsA("Humanoid")
14    if hitHumanoid then
15        -- The mouse was hovering over another player, perform action
View all 23 lines...
0
Im rlly happy thank you so so so much! 3dwardIsBack -10 — 6y
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 — 6y
0
Oh ye and one more thing 3dwardIsBack -10 — 6y
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 — 6y
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 — 6y
0
To push the player back, check out the bodymovers: https://wiki.roblox.com/index.php?title=API:Class/BodyMover chomboghai 2044 — 6y
0
yay Now the target also sits now I will do the bodymove part now 3dwardIsBack -10 — 6y
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 — 6y
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 — 6y
0
Don't forget to select 'Accept answer' so other users know this question has been answered! chomboghai 2044 — 6y
0
ye I do it directly when Im ready 3dwardIsBack -10 — 6y
0
Can you post on this thread with the code formatted please? chomboghai 2044 — 6y
0
the code that I have right now? 3dwardIsBack -10 — 6y
0
I tried to understand the WIKI but I don't get it 3dwardIsBack -10 — 6y
0
So now I got it so far but its pretty bad it looks liek the target gets teleported not even pushed 3dwardIsBack -10 — 6y
Ad

Answer this question