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

How to go higher as the player is in the air?

Asked by 5 years ago
Edited 5 years ago

How can i make it so when the player presses space, it jumps and then if the player holds space it goes higher until it reaches a limit which then falls slowly on the ground ?

Example of that can be SW:Jedi Knight - Jedi Academy where when the player presses space it jumps and when the player holds space it keeps going higher until it reaches a point.

I don't quite know how to use bodygyro, bodyposition, CFrame, etc. so if it requires something with maths, can i get an example ?

( I don't want you to script it for me, because where is the joy of scripting if someone else did it for you. )

1
you could try alignposition, shouldn't require too much math (bodymovers will be deprecated in the (near) future) User#20388 0 — 5y
0
or you could use LineForce User#23365 30 — 5y

1 answer

Log in to vote
0
Answered by
LawlR 182
5 years ago

I know you didn't want anyone to script it for you, but here's one way to do it. Spoilers ahead.

Put this in a local script at StarterPlayer > StarterCharacterScripts. Or put it in a server script if you have an ability that lets other players disturb the 'going uppy' ability and change "Player" to script.Parent.

local Player = game:GetService("Players").LocalPlayer
local UserInputService = game:GetService("UserInputService")
local Holding = false
local RunService = game:GetService("RunService")
local MaxTimeHolding = 2
local Debounce = false

UserInputService.InputBegan:Connect(function(key, typing)
    if key.KeyCode == Enum.KeyCode.Space and typing == false and Debounce == false then
        Debounce = true
        Holding = true
        print("Holding space")
        local BodyForce = Instance.new("BodyForce") -- Not sure whether this is the best force to use, but it works.
        BodyForce.Force = Vector3.new(0, 2000, 0) -- Bascially simulates low gravity.
        BodyForce.Parent = Player.Character.Torso -- Don't think it matters which body part it is. Might have to change Force if it's a different body part.
        local TimeHolding = 0
        local Timer = RunService.Heartbeat:Connect(function(step)
            if TimeHolding < MaxTimeHolding then
                TimeHolding = TimeHolding + 0.04 -- Approximately MaxTimeHolding seconds (I think). Depends on player's fps.
                print(TimeHolding .. " Time Holding")
            else
            end
        end)
        local Landed = Player.Character.Humanoid.StateChanged:Connect(function(state) -- So that the player can't get the effect while in the air.
            if state == Enum.HumanoidStateType.Landed then
                Debounce = false
                Holding = false
                print("Landed")
            end
        end)
        spawn(function()
            while true do
                wait()
                if TimeHolding >= 2 or Holding == false then
                    Timer:Disconnect() -- Don't want it to loop forever.
                    print("Stopped timer")
                    Holding = false
                    if BodyForce then
                        BodyForce:Destroy()
                    end
                    break
                end
            end
        end)
    end
end)

UserInputService.InputEnded:Connect(function(key, typing)
    if key.KeyCode == Enum.KeyCode.Space and typing == false and Holding == true then
        Holding = false
        print("Let go of space")
    end
end)

Can't yet figure out how to disconnect from Landed without breaking it. Maybe someone can comment on how to.

0
Thanks, that works just as I needed it, now i just got to learn what is happening and how it works. :) richboifexekappa 89 — 5y
Ad

Answer this question