How to prevent my script from working on the ground

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?

I want my player to gain velocity in the air after running and jumping, like so:

ezgif-2-0682b34fca

Here is my code:

local UserInputService = game:GetService("UserInputService")
local player = game.Players.LocalPlayer

local LongJumpAnimation = script:WaitForChild('Animation')

-- Settings
local key = Enum.KeyCode.F -- key to trigger dash
local velocity = 7000 -- dash speed
local debounce = false -- debounce, do not set to true
local cooldown = 0.1 -- cooldown after dash
local duration = 0.5 -- dash duration
--

local function Dash()
	local character = player.Character
	if character and not debounce then
		debounce = true
		-- now we begin the dash script here

		local humanoid = character.Humanoid
		local HRP = character.HumanoidRootPart -- HRP stands for HumanoidRootPart

		local loadedAnimation = humanoid.Animator:LoadAnimation(LongJumpAnimation)

		local dashDirection = nil
		local moveDirection = humanoid.MoveDirection
		local lookVector = HRP.CFrame.LookVector
		local minusVelocity = -velocity -- in CFrame, if we set Z to positive, player will go move backward
		-- instead of forward

		-- checking if player is on ground, not floating
		local isOnGround = Enum.Material.Air ~= humanoid.FloorMaterial

		if isOnGround then


			if moveDirection == Vector3.new(0,0,0) then -- if player is not currently moving/walking
				dashDirection = HRP.Position + Vector3.new(lookVector.X, 0, lookVector.Z)
			else -- if player are currently moving/walking
				dashDirection = HRP.Position + Vector3.new(moveDirection.X, 0, moveDirection.Z)
			end

			-- using bodygyro to rotate player to the dash direction smoothly
			local bodyGyro = Instance.new("BodyGyro")
			bodyGyro.Parent = HRP
			bodyGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
			bodyGyro.D = 0 -- D is the dampening
			bodyGyro.P = 500000 -- P is aggressiveness
			bodyGyro.CFrame = CFrame.lookAt(HRP.Position, dashDirection) -- Making player look at the dash direction

			local attachment = Instance.new("Attachment")
			attachment.Parent = HRP

			-- now using VectorForce to move player forward
			local vectorForce = Instance.new("VectorForce")
			vectorForce.Parent = HRP
			-- VectorForce need attachment to tell where is player looking at
			vectorForce.Attachment0 = attachment
			vectorForce.Force = Vector3.new(0,0,minusVelocity) -- now it will move player forward as the settings

			loadedAnimation:Play() -- play the dash animation
			humanoid.AutoRotate = false -- prevent player to rotate by themselves

			wait(duration)

			humanoid.AutoRotate = true

			vectorForce:Destroy()
			bodyGyro:Destroy()
			attachment:Destroy()

			--wait(duration) -- give some time before stopping the dash animation
			loadedAnimation:Stop()

		end


		--
		wait(cooldown)
		debounce = false
	end
end

UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == key then
		Dash()
	end
end)
  1. What is the issue?

Currently, the player can long jump without running and jumping:

ezgif-2-4f405e61dd

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I have messed around with this line in the local script:

	local isOnGround = Enum.Material.Air ~= humanoid.FloorMaterial
--- if isOnGround then, *insert velocity stuff here*	

I believe that line is what would fix my problem, I simply just do not know what to put there.

If you need more details, please ask! Thanks.

1 Like

You could try getting the HumanoidStateType and use that as a check if the Humanoid Is Jumping

-- get humanoid
--code
if humanoid:GetState() == Enum.HumanoidStateType.Jumping then
-- code
end
2 Likes

Good idea, but it won’t trigger. Maybe it’s too brief?

That is what the docs state.

This is because right afterwards the humanoid is freefalling

1 Like

The animation and velocity trigger fine with a freefall, thank you.

-- get humanoid
--code
if humanoid:GetState() == Enum.HumanoidStateType.Freefall then
-- *insert velocity stuff here*	
end

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