Jump Script Improvements

Hey, I am currently working on a jump script and if it was possible to add proper air control to the script. I was also wondering if there where any general improvements you could suggest to the script. Thanks.

local module = {}

local RunService = game:GetService("RunService")
local CollectionService = game:GetService("CollectionService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")

local char = script.Parent.Parent
local part = script.Parent.Parent.HumanoidRootPart
local jcancel = false
local debounce = false
local MAXJUMP = 20
local JUMPSPEED = 100
local distance
local heightChange
local raystepper

function stepper(step)
	if char:FindFirstChild("HumanoidRootPart") and CollectionService:HasTag(char, "R6Ragdoll") == false then
		local caster = char.HumanoidRootPart
		local rayOrigin = caster.Position
		local rayDirection = caster.CFrame.UpVector * -3
		local rayDirection2 = caster.CFrame.UpVector * 4



		local raycastParams = RaycastParams.new()
		raycastParams.FilterDescendantsInstances = {char}
		raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
		local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
		local raycastResult2 = workspace:Raycast(rayOrigin, rayDirection2, raycastParams)

		if raycastResult then
			if debounce == true or jcancel == true then
				jcancel = false
				debounce = false
			end	
		end

		if raycastResult2 then
			jcancel = true
		end
	end
end


raystepper = RunService.Heartbeat:Connect(stepper)

function module.jump()
	local startPos = part.Position.Y
	local currentPos = part.Position.Y
	local velocity = Instance.new("BodyVelocity")

	if debounce == false then
		debounce = true
		velocity.Velocity = Vector3.new(0,0,0)
		velocity.Parent = part

		part.BodyVelocity.Velocity = Vector3.new(part.Velocity.X,JUMPSPEED,part.Velocity.Z)
		distance = JUMPSPEED



		heightChange = RunService.Heartbeat:Connect(function(step)
			if part.Position.Y > currentPos then
				distance = distance - (part.Position.Y - currentPos)*6
				print(distance)
				part.BodyVelocity.Velocity = Vector3.new(part.Velocity.X,distance,part.Velocity.Z)
				
				if currentPos >= startPos + MAXJUMP or jcancel == true or distance < 10 then
					velocity:Destroy()
					
					heightChange:Disconnect()
					return "jumpEnd"
				end
				currentPos = part.Position.Y
			end
		end)
	end
end


function module.cancel()
	jcancel = true
end


return module

2 Likes

Quick question, what does this do? It seems out of place.

1 Like

Oh, my bad. I forgot to remove that from when I was testing something earlier.

1 Like