local UserInputService = game:GetService("UserInputService") --gets userinput
local char = script.Parent --gets character
local hum = char:WaitForChild("Humanoid") --gets humanoid in character
local prevjump = tick() --Sets the prev jump time to the tick when prevjump is used
local IsDoubleJumping = false --The player has not double jumped
hum.StateChanged:Connect(function(oldState,newState) --If the humanoid changed its state then
if newState == Enum.HumanoidStateType.Landed then --if the player has just landed then player can't double jump
IsDoubleJumping = false
elseif newState == Enum.HumanoidStateType.Jumping then --if the player has jumped then
prevjump = tick() --sets prevjump to this tick
end
end)
UserInputService.InputBegan:Connect(function(input) --when a player presses a key
if input.KeyCode == Enum.KeyCode.Space then --if the keycode is space then
if tick() - prevjump <= 0.25 then --if the current time - prevjump time is less than or = to 0.25 meaning its been less than 0.25 seconds since hes jumped then
if hum:GetState() == Enum.HumanoidStateType.Freefall and not IsDoubleJumping then --if the player is freefalling and he hasn't double jumped yet then
IsDoubleJumping = true --it sets doublejump to true meaning he just double jumped
hum:ChangeState(Enum.HumanoidStateType.Jumping) --Changes the state of the humanoid to jumping again
end
end
end
end)
With the comments, did I explain it well? Also, is it just me? I think I am very stupid because when I try and make code myself its rlly bad and bugs a lot but when I check someone elses code and it looks great I understand it so well and I am like "Why didn't I think of that?" I wish I could combine everything I know into a non-buggy code and I started coding like 2 weeks ago, and I still can barely form code with CODE I ALREADY KNOW! is this normal for beginners? If not, how can I help this?
** You are now Level 9! **