Coyote time and Jump Buffer for platformer games

Coyote time is a brief period of time after running off a platform where the game will still register the player pressing the jump button .

Jump Buffer allows the player to input the jump button and still have a successful jump even if they mistimed their input.

Roblox seems to have these things implemented by just almost at an insignificant amount. This code in a local script placed in StarterCharacterScripts allows the player to adjust these times and make their platforming games more forgiving.

--!strict
repeat task.wait() until script.Parent
if not game:IsLoaded() then game.Loaded:Wait() end

local Players: Players = game:GetService("Players")
local UIS: UserInputService = game:GetService("UserInputService")

local Player: Player = Players.LocalPlayer:: Player
local Character: Model = Player.Character or Player.CharacterAdded:Wait()
local Humanoid: Humanoid = Character:FindFirstChild("Humanoid"):: Humanoid

local CoyoteTime: boolean = false
local Buffer:boolean = false

local buffer_time: number = 0.3
local coyote_time: number = 0.125

Humanoid.StateChanged:Connect(function(oldState: Enum.HumanoidStateType, newState: Enum.HumanoidStateType)

	if oldState == Enum.HumanoidStateType.Running and newState == Enum.HumanoidStateType.Freefall and CoyoteTime == false then

		CoyoteTime = true
		task.wait(coyote_time)
		CoyoteTime = false
	end
end)

UIS.InputBegan:Connect(function(input: InputObject, process: boolean)
	if process then return end
	if input.KeyCode == Enum.KeyCode.Space then
		if Humanoid.FloorMaterial == Enum.Material.Air and Buffer == false then
			spawn(function()
				Buffer = true

				spawn(function()
					repeat 
						if Humanoid:GetState() == Enum.HumanoidStateType.Landed then
							Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
							Buffer = false
							return
						end
						task.wait() 
					until Buffer == false
				end)
				task.wait(buffer_time)
				Buffer = false
			end)
			
		end
		if  CoyoteTime == true then
			Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
		end
		
	end
end)
20 Likes