How to check if Player presses key WHILE in a certain state

Hello. I’m trying to make a ground pound system, and for that i made a function for Humanoid.FreeFalling, then i added a InputBegan function inside of it. Basically when the player presses a key while falling, it should create a linear velocity in the HumanoidRootPart of the Player (which it does), then delete it when the player hits the ground. However, even when i press Shift while not falling, it still does the function. I don’t understand why it’s doing that.

--//Services//--
local Players = game:GetService("Players")
local UIS = game:GetService("UserInputService")
--//Variables//--
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Root = Character:WaitForChild("HumanoidRootPart")
local Humanoid = Character:WaitForChild("Humanoid")
--//Function//--
local function onFalling()
	local function onInputBegan(Input)
		if Input.KeyCode == Enum.KeyCode.LeftShift then
			local Velocity = Instance.new("LinearVelocity", Root)
			Velocity.VectorVelocity = Vector3.new(0, -20, 0)
			local function onHitGround(Hit)
				if Hit:IsA("BasePart") or Hit:IsA("Part") or Hit:IsA("MeshPart") or Hit:IsA("UnionOperation") then
					Velocity:Destroy()
					print("Done")
				end
			end
			Root.Touched:Connect(onHitGround)
		end
	end
	UIS.InputBegan:Connect(onInputBegan)
end
--//Connection//--
Humanoid.FreeFalling:Connect(onFalling)
5 Likes

Hi friend, the way you doing is wrong. The biggest problem is you need a new connection to land. I changed your code quite a bit to what I think is an uncomplicated way.

--//Services//--
local Players = game:GetService("Players")
local UIS = game:GetService("UserInputService")
--//Variables//--
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Root = Character:WaitForChild("HumanoidRootPart")
local Humanoid = Character:WaitForChild("Humanoid")
--//Function//--


local function onFalling()
	if UIS:IsKeyDown(Enum.KeyCode.LeftShift) then
		if Root:FindFirstChild("LinearVelocity") == nil then
			local Velocity = Instance.new("LinearVelocity", Root)
			Velocity.VectorVelocity = Vector3.new(0, -20, 0)
			print("Created ",Velocity)
		end
	end
end
--//Connection//--
Humanoid.FreeFalling:Connect(onFalling)

Humanoid.StateChanged:Connect(function(oldState, newState)
	if newState == Enum.HumanoidStateType.Landed then
		local linearVelocity = Root:FindFirstChild("LinearVelocity")
		if linearVelocity then
			linearVelocity:Destroy()
			print("LinearVelocity instance destroyed.")
		end
	end
end)
3 Likes

This another code if anyone is looking to way to stop a freefall using shift:

--//Services//--
local Players = game:GetService("Players")
local UIS = game:GetService("UserInputService")
--//Variables//--
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Root = Character:WaitForChild("HumanoidRootPart")
local Humanoid = Character:WaitForChild("Humanoid")
--//Function//--

-- Variable to Connect/Disconnect Shift Listener
local conWaitForShift

local isFalling = nil

-- Set the fall velocity if shift is pressed
local fallVelocity = -10

function waitForShift(input)	
	if UIS:IsKeyDown(Enum.KeyCode.LeftShift) and isFalling then
		if Root:FindFirstChild("LinearVelocity") == nil then
			local Velocity = Instance.new("BodyVelocity", Root)
			Velocity.Velocity = Vector3.new(0, fallVelocity, 0)			
		end
	end
end

-- Check if the State changed from FreeFall to Landed

Humanoid.StateChanged:Connect(function(oldState, newState)
	if newState == Enum.HumanoidStateType.Freefall then
		print("Free Fall")
		isFalling = true
		conWaitForShift = UIS.InputBegan:Connect(function(input)
			waitForShift(input)
		end)
	end
	if newState == Enum.HumanoidStateType.Landed then
		print("Landed")
		isFalling = false
		if Root:FindFirstChildOfClass("BodyVelocity") then
			local Velocity = Root:FindFirstChildOfClass("BodyVelocity")
			Velocity:Destroy()			
		end
		conWaitForShift:Disconnect()
	end
end)

Wow, it works like a charm! The first script you posted didn’t work, but this one does! Thanks!

1 Like

There’s a problem tho, when i press Shift multiple times it does it multiple times. How do i fix that?

1 Like

Please post what code are you using.

--//Services//--
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UIS = game:GetService("UserInputService")
--//Variables//--
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Root = Character:WaitForChild("HumanoidRootPart")
local Woosh = Root:WaitForChild("ND_Woosh")
local Crumble = Root:WaitForChild("ND_Crumble")
local Pound = Root:WaitForChild("ND_Pound")
local LowerTorso = Character:WaitForChild("LowerTorso")
local Humanoid = Character:WaitForChild("Humanoid")
local StartAnim = script:WaitForChild("StartAnim")
local IdleAnim = script:WaitForChild("IdleAnim")
local HitAnim = script:WaitForChild("HitAnim")
local Start = Humanoid:LoadAnimation(StartAnim)
local Idle = Humanoid:LoadAnimation(IdleAnim)
local Hit = Humanoid:LoadAnimation(HitAnim)
local Particles = ReplicatedStorage.Particles
local Dust = Particles.Dust
local Stars = Particles.Stars
local conWaitForShift
local isFalling = nil
local fallVelocity = -100
--//Function//--
local function waitForShift(input)	
	if UIS:IsKeyDown(Enum.KeyCode.LeftShift) or UIS:IsKeyDown(Enum.KeyCode.ButtonL2) and isFalling then
		if Root:FindFirstChild("BodyVelocity") == nil then
			Woosh:Play()
			Humanoid.WalkSpeed = 0
			Start.Priority = Enum.AnimationPriority.Action4
			Start:Play()
			Root.Anchored = true
			task.wait(0.4)
			Idle:Play()
			Root.Anchored = false
			local Velocity = Instance.new("BodyVelocity", Root)
			Velocity.Velocity = Vector3.new(0, fallVelocity, 0)			
		end
	end
end
-----------------------------------------------------
local function onStateChanged(_, newState)
	if newState == Enum.HumanoidStateType.Freefall then
		isFalling = true
		conWaitForShift = UIS.InputBegan:Connect(function(input)
			waitForShift(input)
		end)
	end
	if newState == Enum.HumanoidStateType.Landed then
		isFalling = false
		if Root:FindFirstChildOfClass("BodyVelocity") then
			Idle:Stop()
			Hit:Play()
			Pound:Play()
			Crumble:Play()
			local DustPRC = Dust:Clone()
			DustPRC.Parent = LowerTorso
			local StarPRC = Stars:Clone()
			StarPRC.Parent = LowerTorso
			local Velocity = Root:FindFirstChildOfClass("BodyVelocity")
			Velocity:Destroy()	
			task.wait(0.75)
			Humanoid.WalkSpeed = 26
			Hit:Stop()
			DustPRC:Destroy()
			StarPRC:Destroy()
		end
		conWaitForShift:Disconnect()
	end
end
--//Connection//--
Humanoid.StateChanged:Connect(onStateChanged)

It is a logical nightmare to debug but seems fixed. Please remember to mark as solution.

--//Services//--
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UIS = game:GetService("UserInputService")
--//Variables//--
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Root = Character:WaitForChild("HumanoidRootPart")
local Woosh = Root:WaitForChild("ND_Woosh")
local Crumble = Root:WaitForChild("ND_Crumble")
local Pound = Root:WaitForChild("ND_Pound")
local LowerTorso = Character:WaitForChild("LowerTorso")
local Humanoid = Character:WaitForChild("Humanoid")
local StartAnim = script:WaitForChild("StartAnim")
local IdleAnim = script:WaitForChild("IdleAnim")
local HitAnim = script:WaitForChild("HitAnim")
local Start = Humanoid:LoadAnimation(StartAnim)
local Idle = Humanoid:LoadAnimation(IdleAnim)
local Hit = Humanoid:LoadAnimation(HitAnim)
local Particles = ReplicatedStorage.Particles
local Dust = Particles.Dust
local Stars = Particles.Stars
local conWaitForShift
local isFalling = nil
local fallVelocity = -100
local shiftCount = 0 -- Shift control

--//Function//--
local function waitForShift(input)	
	if UIS:IsKeyDown(Enum.KeyCode.LeftShift) or UIS:IsKeyDown(Enum.KeyCode.ButtonL2) and isFalling then
		if Root:FindFirstChild("BodyVelocity") == nil and shiftCount == 1 then -- Adding the shift check
			print("Shift pressed times:",shiftCount) -- Debug line please remove
			Woosh:Play()
			Humanoid.WalkSpeed = 0
			Start.Priority = Enum.AnimationPriority.Action4
			Start:Play()
			Root.Anchored = true
			task.wait(0.4)
			Idle:Play()
			Root.Anchored = false
			local Velocity = Instance.new("BodyVelocity", Root)
			Velocity.Velocity = Vector3.new(0, fallVelocity, 0)			
		end
	end
end
-----------------------------------------------------
local function onStateChanged(_, newState)
	if newState == Enum.HumanoidStateType.Freefall then
		isFalling = true
		shiftCount = shiftCount + 1 -- Count shift	
		conWaitForShift = UIS.InputBegan:Connect(function(input)
			waitForShift(input)
		end)
	end
	if newState == Enum.HumanoidStateType.Landed then
		isFalling = false
		shiftCount = 0  -- Zero shift count when landed
		if Root:FindFirstChildOfClass("BodyVelocity") then
			Idle:Stop()
			Hit:Play()
			Pound:Play()
			Crumble:Play()
			local DustPRC = Dust:Clone()
			DustPRC.Parent = LowerTorso
			local StarPRC = Stars:Clone()
			StarPRC.Parent = LowerTorso
			local Velocity = Root:FindFirstChildOfClass("BodyVelocity")
			Velocity:Destroy()	
			task.wait(0.75)
			Humanoid.WalkSpeed = 26
			Hit:Stop()
			DustPRC:Destroy()
			StarPRC:Destroy()
		end
		conWaitForShift:Disconnect()
	end
end
--//Connection//--
Humanoid.StateChanged:Connect(onStateChanged)

It won’t work for some reason?

Yes, but it ONLY says "Shift pressed times: 1", not "Shift pressed times: 2" or “Shift pressed times: 3”, etc. And when i press shift multiple times it says like this "Shift pressed times: 1 (x2)" then "Shift pressed times: 1 (x3)" , etc.

Is the task. Wait()

			task. Wait(0.4)
			Idle:Play()

If you remove task. Wait(0.4) will work.

Lets try again, this time I will count shift before the task.wait() :

--//Services//--
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UIS = game:GetService("UserInputService")
--//Variables//--
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Root = Character:WaitForChild("HumanoidRootPart")
local Woosh = Root:WaitForChild("ND_Woosh")
local Crumble = Root:WaitForChild("ND_Crumble")
local Pound = Root:WaitForChild("ND_Pound")
local LowerTorso = Character:WaitForChild("LowerTorso")
local Humanoid = Character:WaitForChild("Humanoid")
local StartAnim = script:WaitForChild("StartAnim")
local IdleAnim = script:WaitForChild("IdleAnim")
local HitAnim = script:WaitForChild("HitAnim")
local Start = Humanoid:LoadAnimation(StartAnim)
local Idle = Humanoid:LoadAnimation(IdleAnim)
local Hit = Humanoid:LoadAnimation(HitAnim)
local Particles = ReplicatedStorage.Particles
local Dust = Particles.Dust
local Stars = Particles.Stars
local conWaitForShift
local isFalling = nil
local fallVelocity = -100
local shiftCount = 0 -- Shift control

--//Function//--
local function waitForShift(input)	
	if UIS:IsKeyDown(Enum.KeyCode.LeftShift) or UIS:IsKeyDown(Enum.KeyCode.ButtonL2) and isFalling then
		if Root:FindFirstChild("BodyVelocity") == nil and shiftCount == 1 then -- Adding the shift check
			print("Shift pressed times:",shiftCount) -- Debug line please remove
			Woosh:Play()
			Humanoid.WalkSpeed = 0
			Start.Priority = Enum.AnimationPriority.Action4
			Start:Play()
			Root.Anchored = true
			shiftCount = shiftCount + 1
			task.wait(0.4)
			Idle:Play()
			Root.Anchored = false
			local Velocity = Instance.new("BodyVelocity", Root)
			Velocity.Velocity = Vector3.new(0, fallVelocity, 0)			
		end
	end
end
-----------------------------------------------------
local function onStateChanged(_, newState)
	if newState == Enum.HumanoidStateType.Freefall then
		isFalling = true
		shiftCount = shiftCount + 1 -- Count shift	
		conWaitForShift = UIS.InputBegan:Connect(function(input)
			waitForShift(input)
		end)
	end
	if newState == Enum.HumanoidStateType.Landed then
		isFalling = false
		shiftCount = 0  -- Zero shift count when landed
		if Root:FindFirstChildOfClass("BodyVelocity") then
			Idle:Stop()
			Hit:Play()
			Pound:Play()
			Crumble:Play()
			local DustPRC = Dust:Clone()
			DustPRC.Parent = LowerTorso
			local StarPRC = Stars:Clone()
			StarPRC.Parent = LowerTorso
			local Velocity = Root:FindFirstChildOfClass("BodyVelocity")
			Velocity:Destroy()	
			task.wait(0.75)
			Humanoid.WalkSpeed = 26
			Hit:Stop()
			DustPRC:Destroy()
			StarPRC:Destroy()
		end
		conWaitForShift:Disconnect()
	end
end
--//Connection//--
Humanoid.StateChanged:Connect(onStateChanged)print("Hello world!")

Thank you! Now it actually works! :+1:


1 Like

Nice, now please mark as solution!!!

I did it now, again thanks! :+1:

1 Like

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