I want to be criticized till I lose hope of life
Here’s a simple Double Jump Script I wrote in 20 mins
--[[ --> // Pseudocode for Double Jump Script
Event for detecting when the player wants to jump
When detected, (With UserInputService ofc)
Let the player Jump and play jump sound
Record this in a variable (+1 Jump)
Check if humanoid state is either FreeFall, Jumping
If JumpRequest called again, force jump (With ChangeState() function of Humanoid) and play sound
After Humanoid Floor Material is NOT air, reset the jump to 0
]]
local Player = game.Players.LocalPlayer
local Humanoid = Player.Character:WaitForChild("Humanoid")
local UIS = game:GetService("UserInputService")
local JSound = script:WaitForChild("jump")
local HumanoidStateChanged
local CanJump = true
local Jumps = 0
local Jumped = false
local function HasReachedGround() --determines if player has reached the ground
if Humanoid.FloorMaterial == Enum.Material.Air then
return false
else
return true
end
end
UIS.JumpRequest:Connect(function() -- Detects whether player wants to jump (Works in all platforms)
if CanJump == false then return end --Dismisses function if player has used up all jumps
if Jumps >= 2 then return end --Dismisses function when player used all jumps too
Jumps = Jumps + 1
CanJump = true --Think of this as 'CanDoubleJump'
if Jumped == true then --If jumped the first time, run this part of the code
if JSound.Playing then
JSound:Stop()
end
JSound:Play() --Play the jump sound
Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
Jumps = Jumps + 1
Jumped = false --We dont want this part of the code to run again
HumanoidStateChanged:Disconnect() --Disconnect the event below this code, player cant jump again
print("Cannot Jump again")
CanJump = false
repeat wait() until(HasReachedGround() == true) --Wait until player has reached the ground
CanJump = true
Jumps = 0 --Reset jump count
return --Dismiss function since we don't want the code below to run again
end
Jumped = true
JSound:Play()
HumanoidStateChanged = Humanoid.StateChanged:Connect(function(Old, New)
if New == Enum.HumanoidStateType.Landed or Enum.HumanoidStateType.Jumping then --If player is in air or has landed, he can jump again
CanJump = false --Cannot jump till cooldown finishes
wait(.1) --Prevent excessive spam
CanJump = true --Can jump again after short cooldown
print("Can jump again!")
end
end)
end)