TerminalVelocity stays capped at 250

Can anyone help? I have made a system where terminal velocity is capped to 250 however I want to make it fall faster when you airdive. I tried to increase the terminal velocity cap to 900 but it stays capped at 250 for some reason? Can anyone help? Sorry my code is a bit long lol

local player = game.Players.LocalPlayer
local UIS = game:GetService("UserInputService")
local char = player.Character or player.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
local rootPart = char:WaitForChild("HumanoidRootPart") -- The root part of the character
local animation = script:WaitForChild("Animation")
local animator = hum:WaitForChild("Animator")
local load = animator:LoadAnimation(animation)
load.Priority = Enum.AnimationPriority.Action
load.Looped = true
local poser = script:WaitForChild("Poser")
local posing = animator:LoadAnimation(poser)

local rep = game:GetService("ReplicatedStorage")
local objectFolder = rep:WaitForChild("ObjectFall")
local objects = {}

-- Populate the objects table with items from the folder
for _, obj in pairs(objectFolder:GetChildren()) do
	table.insert(objects, obj)
end

local canspawn = false
local delaytime = 2
local freefallTime = 5
local start = 0
local spawnConnection
local isAirDiving = false

local cooldown = 0
local cooldownDuration = 0
local airDiveSpeed = 900 -- Adjust this value to control the fall speed during air dive
local function terminalVecolicty(FallCap)
	local maxFallingSpeed = -FallCap
	local part = script.Parent.PrimaryPart -- the vertical velocity of this part shall not go lower than maxFallingSpeed

	game:GetService("RunService").Stepped:Connect(function()
		local v = part.Velocity
		if v.Y < maxFallingSpeed then
			part.Velocity = Vector3.new(v.X, math.max(maxFallingSpeed, v.Y), v.Z)
		end
	end)
end
local function spawnRandomObject()
	if tick() - cooldown >= cooldownDuration then
		if #objects == 0 then return end

		local randomIndex = math.random(1, #objects)
		local objectToSpawn = objects[randomIndex]:Clone()

		local playerPosition = char.PrimaryPart.Position
		local randomRadius = 275
		local randomAngle = math.rad(math.random(0, 359))
		local randomDistance = math.random(252.5, randomRadius)
		local randomOffset = Vector3.new(
			math.cos(randomAngle) * randomDistance,
			100,
			math.sin(randomAngle) * randomDistance
		)
		objectToSpawn.Position = playerPosition + randomOffset

		local randomRotation = CFrame.Angles(
			math.rad(math.random(-30, 30)),
			math.rad(math.random(-15, 15)),
			math.rad(math.random(-85, 85))
		)
		objectToSpawn.CFrame = CFrame.new(objectToSpawn.Position) * randomRotation

		local randomNum = math.random(1, 1.5)
		local randomScale = Vector3.new(
			randomNum,
			randomNum,
			randomNum
		)
		objectToSpawn.Size = objectToSpawn.Size * randomScale
		objectToSpawn.Parent = workspace

		delay(15, function()
			if objectToSpawn and objectToSpawn.Parent == workspace then
				objectToSpawn:Destroy()
			end
		end)

		cooldown = tick()
	end
end

local function stopPosing()
	if isAirDiving then
		posing:Stop()
		isAirDiving = false
		rootPart.Velocity = Vector3.new(rootPart.Velocity.X, 0, rootPart.Velocity.Z) -- Reset vertical speed
	end
end

hum.StateChanged:Connect(function(oldState, newState)
	if newState == Enum.HumanoidStateType.Freefall then
		start = tick()

		delay(freefallTime, function()
			if hum:GetState() == Enum.HumanoidStateType.Freefall then
				canspawn = true

				spawnConnection = game:GetService("RunService").Stepped:Connect(function()
					if tick() - start >= delaytime and hum:GetState() == Enum.HumanoidStateType.Freefall then
						spawnRandomObject()
						start = tick()
					end
				end)

				load:Play()
				terminalVecolicty(250)
				print("Terminal250")
			end
		end)

	elseif newState ~= Enum.HumanoidStateType.Freefall and canspawn then
		load:Stop()
		canspawn = false

		if spawnConnection then
			spawnConnection:Disconnect()
			spawnConnection = nil
		end

		stopPosing() -- Stop the posing animation when leaving Freefall
	end
end)

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift and hum:GetState() == Enum.HumanoidStateType.Freefall and not isAirDiving then
		isAirDiving = true
		terminalVecolicty(900)
		print("Terminal900")
		posing:Play()

		-- Apply downward force to make the player fall faster
		rootPart.Velocity = Vector3.new(rootPart.Velocity.X, -airDiveSpeed, rootPart.Velocity.Z)
	end
end)

UIS.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		stopPosing()
		print("StoppedPosing")
		terminalVecolicty(250)
		print("Terminal250")
	end
end)

Instead of calling the terminalVecolicty function a lot, you should just change a variable located outside of the function, which is used in the math.max thing.

Currently, the setup you have has a memory leak, and has all of the terminalVelocities trying to act at the same time, causing it to limit it to the lowest value.

1 Like

Thank you man! It now works well. :grin:

image
The code works well but now this keeps spamming my output log now however

Can you please provide the code you are using, so I can remedy the issue you encountered.

local maxFallingSpeed = -250
local function terminalVecolicty(maxFallingSpeed)
	
	local part = script.Parent.PrimaryPart -- the vertical velocity of this part shall not go lower than maxFallingSpeed

	game:GetService("RunService").Stepped:Connect(function()
		local v = part.Velocity
		if v.Y < maxFallingSpeed then
			part.Velocity = Vector3.new(v.X, math.max(maxFallingSpeed, v.Y), v.Z)
		end
	end)
end
local function spawnRandomObject()
	if tick() - cooldown >= cooldownDuration then
		if #objects == 0 then return end

		local randomIndex = math.random(1, #objects)
		local objectToSpawn = objects[randomIndex]:Clone()

		local playerPosition = char.PrimaryPart.Position
		local randomRadius = 275
		local randomAngle = math.rad(math.random(0, 359))
		local randomDistance = math.random(252.5, randomRadius)
		local randomOffset = Vector3.new(
			math.cos(randomAngle) * randomDistance,
			100,
			math.sin(randomAngle) * randomDistance
		)
		objectToSpawn.Position = playerPosition + randomOffset

		local randomRotation = CFrame.Angles(
			math.rad(math.random(-30, 30)),
			math.rad(math.random(-15, 15)),
			math.rad(math.random(-85, 85))
		)
		objectToSpawn.CFrame = CFrame.new(objectToSpawn.Position) * randomRotation

		local randomNum = math.random(1, 1.5)
		local randomScale = Vector3.new(
			randomNum,
			randomNum,
			randomNum
		)
		objectToSpawn.Size = objectToSpawn.Size * randomScale
		objectToSpawn.Parent = workspace

		delay(15, function()
			if objectToSpawn and objectToSpawn.Parent == workspace then
				objectToSpawn:Destroy()
			end
		end)

		cooldown = tick()
	end
end

local function stopPosing()
	if isAirDiving then
		posing:Stop()
		isAirDiving = false
		rootPart.Velocity = Vector3.new(rootPart.Velocity.X, 0, rootPart.Velocity.Z) -- Reset vertical speed
	end
end

hum.StateChanged:Connect(function(oldState, newState)
	if newState == Enum.HumanoidStateType.Freefall then
		start = tick()

		delay(freefallTime, function()
			if hum:GetState() == Enum.HumanoidStateType.Freefall then
				canspawn = true

				spawnConnection = game:GetService("RunService").Stepped:Connect(function()
					if tick() - start >= delaytime and hum:GetState() == Enum.HumanoidStateType.Freefall then
						spawnRandomObject()
						start = tick()
					end
				end)

				load:Play()
				maxFallingSpeed = -250
				terminalVecolicty()
				print("Terminal250")
			end
		end)

	elseif newState ~= Enum.HumanoidStateType.Freefall and canspawn then
		load:Stop()
		canspawn = false

		if spawnConnection then
			spawnConnection:Disconnect()
			spawnConnection = nil
		end

		stopPosing() -- Stop the posing animation when leaving Freefall
	end
end)

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift and hum:GetState() == Enum.HumanoidStateType.Freefall and not isAirDiving then
		isAirDiving = true
		maxFallingSpeed = -900
		terminalVecolicty()
		print("Terminal900")
		posing:Play()

		-- Apply downward force to make the player fall faster
		rootPart.Velocity = Vector3.new(rootPart.Velocity.X, -airDiveSpeed, rootPart.Velocity.Z)
	end
end)

UIS.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		stopPosing()
		print("StoppedPosing")
		maxFallingSpeed = -250
		terminalVecolicty()
		print("Terminal250")
	end
end)

this is the changes I made.

Not neccesarily the fix to your current issue, but you shoudl remove all the times that you call terminalVelocity.
Call it a single time, and never again.

yeah will do, makes sense thank you! I removed maxFallingSpeed as a parameter and the output is all good now! However when I tried that before it broke my game so I don’t know if its just a weird lua glitch or what.

Thank u for ur help!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.