Crouch script not working

Self explanatory, my crouch script isn’t working for some reason. Here’s what you need to know. The local script is inside a folder called Movement and the folder is inside StarterCharacterScripts.

Screen Shot 2022-12-10 at 5.05.13 PM

Local Script
-- This is the pc input handler.
local Players = game:GetService("Players")
local UIS = game:GetService("UserInputService")

local Player = Players.LocalPlayer
local Character = Player.Character
local CrouchKey = Enum.KeyCode.C

local Deb = false
-- Checks if Humanoid is Alive.
local function CheckAlive()
	local Hum = script.Parent.Parent:FindFirstChild("Humanoid")
	local Head = script.Parent.Parent:FindFirstChild("Head")
	local HumanRoot = script.Parent.Parent: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 = script.Parent.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 not Character then return end
	
	if input == CrouchKey and CheckAlive() == true then
		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(script.Parent:FindFirstChild("Humanoid"), TweenInfo.new(0.2), {HipHeight = 0.3}):Play()
		end
	end
end)

-- Input ends
UIS.InputEnded:Connect(function(input,gameProcessed)
	if gameProcessed then return end
	if not Character then return end

	if input == CrouchKey and CheckAlive() == true then
		if CheckHead() then
			Deb = false
			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(script.Parent:FindFirstChild("Humanoid"), TweenInfo.new(0.2), {HipHeight = 2}):Play()
		end
	end
end)

This probably will not fix your issue but please use Animator:LoadAnimation() over Humanoid:LoadAnimation().

Deprecating LoadAnimation on Humanoid and AnimationController - Updates / Announcements - DevForum | Roblox

1 Like

Your Character variable will always be nil. You need to add this line after you set the Player and before you set the Character:

Player.CharacterAdded:Wait()
1 Like

Player.CharacterAdded:Wait() Try this after play but before character

2 Likes

It didn’t do anything unless I didn’t use it correctly. I added wait(1.25); and don’t mind the print debug.

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

local Player = Players.LocalPlayer
Player.CharacterAdded:Wait()
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 = script.Parent.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
	
	print("Input began")
	if input == CrouchKey and CheckAlive() then
		print("C Input began")
		if not Deb then
			Deb = true
			local CrouchIdle = Character:WaitForChild("Humanoid").Animator:LoadAnimation(script.CrouchIdle)
			local Animate = Character:WaitForChild("Animate")
			CrouchIdle:Play()
			Animate.walk.WalkAnim.AnimationId = script.CrouchWalk.AnimationId
			game:GetService("TweenService"):Create(script.Parent:FindFirstChild("Humanoid"), TweenInfo.new(0.2), {HipHeight = 0.3}):Play()
		end
	end
end)

-- Input ends
UIS.InputEnded:Connect(function(input,gameProcessed)
	if gameProcessed then return end
	print("Input began")
	if input == CrouchKey and CheckAlive() == true then
		if CheckHead() then
			Deb = false
			local Animate = Character:WaitForChild("Animate")
			local CrouchIdle = Character:WaitForChild("Humanoid").Animator:LoadAnimation(script.CrouchIdle)
			CrouchIdle:Stop()
			Animate.walk.WalkAnim.AnimationId = "rbxassetid://10898889275"
			game:GetService("TweenService"):Create(script.Parent:FindFirstChild("Humanoid"), TweenInfo.new(0.2), {HipHeight = 2}):Play()
		end
	end
end)

None of the prints fired by the way.

Replying to my self after debugging my script. Seems that it isn’t going through CharacterAdded

Here’s proof with the script in debug mode:

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

local Player = Players.LocalPlayer
print("Behind CharacterAdded")
Player.CharacterAdded:Wait()
print("Passed CharacterAdded")
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
print("Passed CheckAlive")
-- Checks if head is in the way
local function CheckHead()
	if not CheckAlive() then return end

	local rayOrigin = script.Parent.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
print("Passed CheckHead")
-- Input begans, crouch idle
UIS.InputBegan:Connect(function(input, gameProcessed)
	if gameProcessed then return end

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

As you can see a put prints behind every function every 10 or more lines. The output is

  19:00:50.442  Behind CharacterAdded  -  Client - pcInputs:7
  19:01:00.342 

I fixed this problem by doing repeat wait() until Player.Character

I know this isn’t optimal but Player.CharacterAdded:Wait() yielded the script for some reason.

It still isn’t working though, when I press C, it doesn’t print anything but it goes through the UIS.InputBegan and UIS.InputEnded

-- 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 = script.Parent.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 == CrouchKey and CheckAlive() == true then
		if not Deb then
			Deb = true
			local CrouchIdle = Character:WaitForChild("Humanoid").Animator:LoadAnimation(script.CrouchIdle)
			local Animate = Character:WaitForChild("Animate")
			CrouchIdle:Play()
			Animate.walk.WalkAnim.AnimationId = script.CrouchWalk.AnimationId
			game:GetService("TweenService"):Create(script.Parent:FindFirstChild("Humanoid"), TweenInfo.new(0.2), {HipHeight = 0.3}):Play()
		end
	end
end)
-- Input ends
UIS.InputEnded:Connect(function(input,gameProcessed)
	if gameProcessed then return end
	if input == CrouchKey and CheckAlive() == true then
		print("C Input end")
		if CheckHead() then
			Deb = false
			local Animate = Character:WaitForChild("Animate")
			local CrouchIdle = Character:WaitForChild("Humanoid").Animator:LoadAnimation(script.CrouchIdle)
			CrouchIdle:Stop()
			Animate.walk.WalkAnim.AnimationId = "rbxassetid://10898889275"
			game:GetService("TweenService"):Create(script.Parent:FindFirstChild("Humanoid"), TweenInfo.new(0.2), {HipHeight = 2}):Play()
		end
	end
end)

Edit: I fixed it by adding input.Keycode, oopsies the original problem was the character though since it was yielding the script.

Another thing to note:

task.wait() over wait()

In short: wait() is outdated.

1 Like

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