Run Script Jumping Issue

I have this run script however if a player is spamming the jump button the jump animation will play the first jump then just play running animation of the rest of the spam jumping.

-- Services
local userInputService = game:GetService("UserInputService")
local players = game:GetService("Players")
-- Variables
local player = players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
local animator = hum:WaitForChild("Animator")

local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://15720702879"
local track = animator:LoadAnimation(animation)
track.Priority = Enum.AnimationPriority.Movement

local RUN_KEYCODE = Enum.KeyCode.LeftShift
local RUN_SPEED = 40
local WALK_SPEED = 16
-- Togglables
local isSprinting = false -- if player has sprinting toggled
local isMoving = false -- if player is moving
local isInAir = false -- if player is in the air (because of jumping)

local function updateAnimation() -- Updates it based on if the player is moving or if they have sprinting on/off
	if isSprinting and isMoving then
		hum.WalkSpeed = RUN_SPEED
		if not track.IsPlaying then -- makes sure it isnt already playing
			track:Play()
		end
	else
		hum.WalkSpeed = WALK_SPEED
		track:Stop()
	end
end

local function inputBegan(input, processed)
	if processed then return end

	if input.KeyCode == RUN_KEYCODE then
		isSprinting = not isSprinting -- toggles on and off
		updateAnimation()
	end
end

local function moveDirectionChanged()
	isMoving = hum.MoveDirection.Magnitude > 0 -- sets var to true/false depending if they are moving or not
	if not isInAir then
		updateAnimation()
	end
end

userInputService.InputBegan:Connect(inputBegan) -- player presses key (we want them to press shift)

hum:GetPropertyChangedSignal("MoveDirection"):Connect(moveDirectionChanged) -- player moves

-- Additional: Stop sprinting if jumping | Redone
userInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Space then
		track:Stop()
		isInAir = true
		task.wait(.8)
		isInAir = false
	end
end)
1 Like

I edited the part where it detects if you hold down space, as it is only when the input began, so I included inputEnded to set the boolean isInAir to false. It is not perfect, but works, you just have to do same for example for mobile controls too if you want it for mobile players to work too.

-- Additional: Stop sprinting if jumping | Redone
userInputService.InputBegan:Connect(function(input, gameEvent)
	if not gameEvent then
		if input.KeyCode == Enum.KeyCode.Space then
			track:Stop()
			isInAir = true
		end
	end
end)

userInputService.InputEnded:Connect(function(input, gameEvent)
	if not gameEvent then
		if input.KeyCode == Enum.KeyCode.Space then
			task.wait(0.4)
			isInAir = false
		end
	end
end)
2 Likes

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