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

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

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

2 comments

1

you could try alignposition, shouldn't require too much math (bodymovers will be deprecated in the (near) future)

0

or you could use LineForce

1 answer

✓ Accepted answer

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.

1 comment

0

Thanks, that works just as I needed it, now i just got to learn what is happening and how it works. :)

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