Random jump cancelling

Can’t figure this one out.
Why does it randomly cancel my jump?

To replicate:

  1. In StarterPlayer set DevComputerMovementMode to Scriptable
  2. Script in StarterPlayerScripts
-- Services --
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")

-- Variables --
local LocalPlayer = Players.LocalPlayer
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")

local KeyStates = {
	W = false;
	A = false;
	S = false;
	D = false;
}

local velocity = Vector3.new(0, 0, 0)
local vertVelocity = 0

local acceleration = 80
local deacceleration = 16
local groundVelocityCap = 16
local jumpHeight = 100
local gravity = -1

local isSliding = false
local isJumping = false

local moveDirection = Vector3.new(0, 0, 0)
local lastMoveDirection = Vector3.new(0, 0, 0)
local lastHumanoidRootPartLookVector

-- Input Handler --
UserInputService.InputBegan:Connect(function(input, _gameProcessed)
	if _gameProcessed then return end

	if input.KeyCode == Enum.KeyCode.W then
		KeyStates.W = true
	elseif input.KeyCode == Enum.KeyCode.A then
		KeyStates.A = true
	elseif input.KeyCode == Enum.KeyCode.S then
		KeyStates.S = true
	elseif input.KeyCode == Enum.KeyCode.D then
		KeyStates.D = true
	elseif input.KeyCode == Enum.KeyCode.LeftShift then
		isSliding = true
	elseif input.KeyCode == Enum.KeyCode.Space then
		if not isJumping and Humanoid.FloorMaterial ~= Enum.Material.Air then
			isJumping = true
			vertVelocity = jumpHeight
		end
	end
end)

UserInputService.InputEnded:Connect(function(input, _gameProcessed)
	if _gameProcessed then return end

	if input.KeyCode == Enum.KeyCode.W then
		KeyStates.W = false
	elseif input.KeyCode == Enum.KeyCode.A then
		KeyStates.A = false
	elseif input.KeyCode == Enum.KeyCode.S then
		KeyStates.S = false
	elseif input.KeyCode == Enum.KeyCode.D then
		KeyStates.D = false
	elseif input.KeyCode == Enum.KeyCode.LeftShift then
		isSliding = false
	elseif input.KeyCode == Enum.KeyCode.Space then
		isJumping = false
	end
end)

-- RunService Loop --
RunService.Stepped:Connect(function(_currentTime, deltaTime)
	-- Wish direction
	local wishDir = Vector3.new(
		(KeyStates.D and 1 or 0) - (KeyStates.A and 1 or 0),
		0,
		(KeyStates.S and 1 or 0) - (KeyStates.W and 1 or 0)
	)

	if wishDir.Magnitude == 0 then
		wishDir = Vector3.new(0, 0, 0)
	end
	
	-- Acceleration and velocity cap on ground
	if wishDir.Magnitude > 0 and not isSliding then
		velocity = velocity + (wishDir * acceleration * deltaTime)
		if velocity.Magnitude > groundVelocityCap and Humanoid.FloorMaterial ~= Enum.Material.Air then
			velocity = velocity.Unit * groundVelocityCap
		end
	end

	-- Ground deacceleration
	if Humanoid.FloorMaterial ~= Enum.Material.Air and not isSliding and wishDir == Vector3.new(0, 0, 0) then
		if velocity.Magnitude < 0.1 then velocity = Vector3.new(0, 0, 0) end
		velocity = velocity * (1 - deacceleration * deltaTime)
	end
	
	if Humanoid.FloorMaterial ~= Enum.Material.Air then
		if isJumping then
			vertVelocity = jumpHeight
			isJumping = false
		else
			vertVelocity = 0
		end
	else
		vertVelocity = vertVelocity + (gravity * deltaTime)
	end

	-- Move Character and lock orientation when sliding
	if not isSliding then
		moveDirection = Vector3.new(velocity.X, vertVelocity, velocity.Z)
		
		lastMoveDirection = moveDirection
		lastHumanoidRootPartLookVector = HumanoidRootPart.CFrame.LookVector
	else
		moveDirection = Vector3.new(lastMoveDirection.X, vertVelocity, lastMoveDirection.Z)
		HumanoidRootPart.CFrame = CFrame.new(HumanoidRootPart.CFrame.Position, HumanoidRootPart.CFrame.Position + lastHumanoidRootPartLookVector)
		task.delay(.5, function()
			isSliding = false
		end)
	end
	
	HumanoidRootPart.CFrame = HumanoidRootPart.CFrame * CFrame.new(moveDirection * deltaTime)
	LocalPlayer.PlayerGui.ScreenGui.Frame.TextLabel.Text = tostring(velocity.Magnitude)
end)
1 Like

I can’t say this is for sure, but it may be the timing… that the RunService function you have may be actually too early or late. Because I think that .Stepped is ran before the physics simulation. Meaning it may cause the function to run too early, affecting the next jump instead, and using the wrong values at the wrong time. I had an issue with something like this when I did a jump fatigue script quite a bit ago.

Try changing .Stepped to .PreSimulation, if that doesn’t work then it could be because it’s too early, so maybe try .RenderStepped

Task scheduler diagram, may be useful (From roblox docs)

If this timing isn’t it, I would try changing/messing around with your ground deceleration/Acceleration and cap code, because that could also be causing it. (btw you made a typo in your comment and in your code, should be deceleration, srry.)

This part of your code: (No edits made, just pointing out another area of focus

-- Acceleration and velocity cap on ground
	if wishDir.Magnitude > 0 and not isSliding then
		velocity = velocity + (wishDir * acceleration * deltaTime)
		if velocity.Magnitude > groundVelocityCap and Humanoid.FloorMaterial ~= Enum.Material.Air then
			velocity = velocity.Unit * groundVelocityCap
		end
	end

	-- Ground deacceleration
	if Humanoid.FloorMaterial ~= Enum.Material.Air and not isSliding and wishDir == Vector3.new(0, 0, 0) then
		if velocity.Magnitude < 0.1 then velocity = Vector3.new(0, 0, 0) end
		velocity = velocity * (1 - deacceleration * deltaTime)
	end
	
	if Humanoid.FloorMaterial ~= Enum.Material.Air then
		if isJumping then
			vertVelocity = jumpHeight
			isJumping = false
		else
			vertVelocity = 0
		end
	else
		vertVelocity = vertVelocity + (gravity * deltaTime)
	end

I’ve tried RunService.Heartbeat, RunService.Stepped, RunService.PreSimulation, RunService.PostSimulation
Nothing seems to fix the problem, some just make it more intense

Okay, well I’m kind of unsure of your real end goal and of what your trying to do with the ground deceleration, and acceleration code, and I’m probably not that much of a help when it comes to that, so if you can’t find anyone else who knows, maybe give ChatGPT a try, since I’m sure it’ll be able to help you better than I can, since my physics and mathematical tolerance is pretty low lel.

aint using chatgpt, literally random word generator

You’d be surprised. It’s literally a GPU made for mathematically calculations, if anything/one can fix any math or physics issue you have with your code it’s going to be that GPU.

i always try chatgpt before posting here

Okay, well I’m just not able to help you much further, all I can do is say that I believe the issue is somewhere in this area of your code

well, this doesnt help me much

Sorry, I’m just not able to really figure out why it’s doing that, all I can do is give you my advice on where it may be happening.

I’ve managed to fix it.

if Humanoid.FloorMaterial ~= Enum.Material.Air then
		if not isJumping then
			vertVelocity = 0
		end
	else
		vertVelocity = vertVelocity + (gravity * deltaTime)
	end

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