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

Trying to change the body velocity of a player after pressing a certain key?

Asked by 3 years ago

I'm trying to change the body velocity of the player after they press a certain key. Ideally, I wanted it so after the player pressed a certain key they world be flung upwards and then flung downwards back to the ground. Here is my failed attempt.

adebounce = false
local UIS = game:GetService("UserInputService")
local char = script.Parent
local jumpshiftanim = Instance.new("Animation")
jumpshiftanim.AnimationId = "rbxassetid://example"

local keybind = Enum.KeyCode.LeftShift
local canjump = true

UIS.InputBegan:Connect(function(input,gameprocessed)
    if gameprocessed then return end
    if not canjump then return end

    if input.KeyCode == keybind then
        canjump = false

        local playAnim = char.Humanoid:LoadAnimation(jumpshiftanim)
        playAnim:Play()

        local jump = Instance.new("BodyVelocity")
        local jump2 = Instance.new("BodyVelocity")
        jump.Velocity = Vector3.new(0,50,0)
        jump.Parent = char.HumanoidRootPart 
        adebounce = true
        jump2.Velocity = Vector3.new(0,-700,0)
        jump2.Parent = char.HumanoidRootPart

        for count = 1,8 do
            wait(0,1)
        end
        playAnim:Stop()
        jump:Destroy()
        wait(6)
        jump2:Destroy()
        wait(5)
        canjump = true
    end
end)

1 answer

Log in to vote
1
Answered by 3 years ago

Thing is, you're using .Velocity wrong. .Velocity determines the target velocity towards which force will be exerted.

See this to know more: https://developer.roblox.com/en-us/api-reference/class/BodyVelocity

So here's how I would go about exerting BodyVelocity to my character...

local char = script.Parent

local jump = Instance.new("BodyVelocity")
jump.MaxForce = Vector3.new(1000,1000,1000) --Maximum force to be exerted in each axis
jump.Velocity = char.HumanoidRootPart.CFrame.UpVector * 1000 --exerts 1000 upwards
jump.Parent = char.HumanoidRootPart

wait(1)

jump.Velocity = char.HumanoidRootPart.CFrame.UpVector * -10000 --now flings them backward
Ad

Answer this question