InputBegan not working but InputEnded working

Self explanatory. I made a crouch script and InputBegan is not firing for my input (C) for some reason

-- This is the pc input handler.
wait(1.25);
local Players = game:GetService("Players")
local UIS = game:GetService("UserInputService")

local Player = Players.LocalPlayer

repeat wait() until Player.Character

local Character = Player.Character
local CrouchKey = Enum.KeyCode.C
local Deb = false
-- Checks if Humanoid is Alive.
local function CheckAlive()
	local Hum = Character:FindFirstChild("Humanoid")
	local Head = Character:FindFirstChild("Head")
	local HumanRoot = Character:FindFirstChild("HumanoidRootPart")
	if Hum and Hum.Health > 0 and Head and HumanRoot then
		return true
	else
		return false
	end
end

-- Checks if head is in the way
local function CheckHead()
	if not CheckAlive() then return end

	local rayOrigin = Character.Head.Position
	local rayDirection = Vector3.new(0, 2, 0)

	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {script.Parent}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)

	if raycastResult then
		local hitPart = raycastResult.Instance
		if hitPart.Transparency == 0 then
			return false
		else
			return true
		end
	else
		return true
	end
end

-- Input begans, crouch idle
UIS.InputBegan:Connect(function(input, gameProcessed)
	if gameProcessed then return end
	if input.KeyCode == Enum.KeyCode.C and CheckAlive() == true then
		print("C")
		if not Deb then
			Deb = true
			local CrouchIdle = Character:WaitForChild("Humanoid"):LoadAnimation(script.CrouchIdle)
			local Animate = Character:WaitForChild("Animate")
			CrouchIdle:Play()
			Animate.walk.WalkAnim.AnimationId = script.CrouchWalk.AnimationId
			game:GetService("TweenService"):Create(Character:FindFirstChild("Humanoid"), TweenInfo.new(0.2), {HipHeight = 0.3}):Play()
			Deb = false
		end
	end
end)
-- Input ends
UIS.InputEnded:Connect(function(input,gameProcessed)
	if gameProcessed then return end
	if input.KeyCode == Enum.KeyCode.C and CheckAlive() == true then
		print("C end")
		if CheckHead() then
			local Animate = Character:WaitForChild("Animate")
			local CrouchIdle = Character:WaitForChild("Humanoid"):LoadAnimation(script.CrouchIdle)
			CrouchIdle:Stop()
			Animate.walk.WalkAnim.AnimationId = "rbxassetid://10898889275"
			game:GetService("TweenService"):Create(Character:FindFirstChild("Humanoid"), TweenInfo.new(0.2), {HipHeight = 2}):Play()
		end
	end
end)

May we know, where is your script located?
It could be stuck on this line of code

I should’ve gave more information but I don’t think thats the problem.
I fixed it here: Crouch script not working - #7 by Remingling


pcInputs is where the script is

Just tested it out on my own in studio, and it works well.
Could you re-check if the scripts are actually being putted into your character upon loading?

1 Like

Couple things that needs a bit of fixing

  • One - Instead
    repeat wait() until Player.Character
    you can just have
    local Character = Player.Character or Player.CharacterAdded:Wait()

  • Two - CrouchKey on top of the script is not being used so make sure
    to replace it in the if statement like this if input.KeyCode == CrouchKey and CheckAlive() == true then end

  • Three - You can called tween service on top of the script instead of getting the service each time or and make the Crouch Tween on top of the script instead of creating the tween

Try, Loading the Animation Inside of the Humanoid.Animator if it’s r15 (Not really sure if r6 has it)

1 Like

I tried Humanoid.Animator and it didn’t work well so I used the depricated version since it always worked for me. I will change it back and try to fix it. I tried Player.CharacterAdded:Wait() and it yielded the code so I just used to unoptimal version.


-- Services
local Players = game:GetService("Players")
local UIS = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")

local Player = Players.LocalPlayer 

local Character = Player.Character or Player.CharacterAdded:Wait()

local CrouchKey = Enum.KeyCode.C
local Deb = false
-- Checks if Humanoid is Alive.
local function CheckAlive()
	local Hum = Character:FindFirstChild("Humanoid")
	local Head = Character:FindFirstChild("Head")
	local HumanRoot = Character:FindFirstChild("HumanoidRootPart")
	if Hum and Hum.Health > 0 and Head and HumanRoot then
		return true
	else
		return false
	end
end

-- Checks if head is in the way
local function CheckHead()
	if not CheckAlive() then return end

	local rayOrigin = Character.Head.Position
	local rayDirection = Vector3.new(0, 2, 0)

	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {script.Parent}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)

	if raycastResult then
		local hitPart = raycastResult.Instance
		if hitPart.Transparency == 0 then
			return false
		else
			return true
		end
	else
		return true
	end
end

-- Input begans, crouch idle
UIS.InputBegan:Connect(function(input, gameProcessed)
	if gameProcessed then return end
	if input.KeyCode == CrouchKey and CheckAlive() == true then
		print("C")
		if not Deb then
			Deb = true
			--local CrouchIdle = Character:WaitForChild("Humanoid"):LoadAnimation(script.CrouchIdle)
			--local Animate = Character:WaitForChild("Animate")
			--CrouchIdle:Play()
			--Animate.walk.WalkAnim.AnimationId = script.CrouchWalk.AnimationId
			TweenService:Create(Character:FindFirstChild("Humanoid"), TweenInfo.new(0.2), {HipHeight = 0.3}):Play()
			Deb = false
		end
	end
end)
-- Input ends
UIS.InputEnded:Connect(function(input,gameProcessed)
	if gameProcessed then return end
	if input.KeyCode == CrouchKey and CheckAlive() == true then
		print("C end")
		if CheckHead() then
			--local Animate = Character:WaitForChild("Animate")
			--local CrouchIdle = Character:WaitForChild("Humanoid"):LoadAnimation(script.CrouchIdle)
			--CrouchIdle:Stop()
			--Animate.walk.WalkAnim.AnimationId = "rbxassetid://10898889275"
			TweenService:Create(Character:FindFirstChild("Humanoid"), TweenInfo.new(0.2), {HipHeight = 2}):Play()
		end
	end
end)

This worked just fine for me without the animations of course they are comment out since i don’t have the animations I gave it try the inputBegan and inputEnded works just fine for me

I just found out this a studio bug and it works fine when you actually play it, i’ll just solution the mishappens in my script

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