C only works when not chatting?

So I have this crouch script, but it activates when pressing c, any way around that?

crawling = false
local humanoid = script.Parent.Humanoid
crawlAnim = nil
crawlAnimIdle = nil
game:GetService("UserInputService").InputBegan:connect(function(input)
	if input.KeyCode == Enum.KeyCode.C then
		if crawling == true then
			crawling = false
			crawlAnim:Stop()
			crawlAnim:Destroy()
			crawlAnimIdle:Stop()
			crawlAnimIdle:Destroy()
			humanoid.WalkSpeed = humanoid.WalkSpeed*2
			humanoid.Parent.Head.Running.PlaybackSpeed = 1.85
		else
			crawling = true
			crawlAnim = humanoid:LoadAnimation(script.CrawlAnim)
			crawlAnimIdle = humanoid:LoadAnimation(script.CrawlAnimIdle)
			humanoid.WalkSpeed = humanoid.WalkSpeed/2
			humanoid.Parent.Head.Running.PlaybackSpeed = 1
		end
	end
end)

while wait()do
	if crawling == true then
		crawlAnim:AdjustSpeed(humanoid.WalkSpeed/8)
		if humanoid.MoveDirection ~= Vector3.new(0,0,0)then
			if crawlAnim.IsPlaying ~= true then
				crawlAnim:Play()
			end
		else
			crawlAnim:Stop()
			crawlAnimIdle:Play()
		end
	end
end

InputBegan has a second parameter, gameProcessedEvent. Check if it’s false before checking the KeyCode or handle both at the same time (I prefer the former over the latter). GameProcessedEvent describes if something internally is listening to input: chatting is a focused textbox, therefore internally listening input.

3 Likes

I tried this but now it doesn’t work at all.

crawling = false
local humanoid = script.Parent.Humanoid
crawlAnim = nil
crawlAnimIdle = nil

function onKeyPress(input, gameProcessedEvent)
	if input.KeyCode == Enum.KeyCode.C and not gameProcessedEvent then
		if crawling == true then
			crawling = false
			crawlAnim:Stop()
			crawlAnim:Destroy()
			crawlAnimIdle:Stop()
			crawlAnimIdle:Destroy()
			humanoid.WalkSpeed = humanoid.WalkSpeed*2
			humanoid.Parent.Head.Running.PlaybackSpeed = 1.85
		else
			crawling = true
			crawlAnim = humanoid:LoadAnimation(script.CrawlAnim)
			crawlAnimIdle = humanoid:LoadAnimation(script.CrawlAnimIdle)
			humanoid.WalkSpeed = humanoid.WalkSpeed/2
			humanoid.Parent.Head.Running.PlaybackSpeed = 1
		end
	end
end

while wait()do
	if crawling == true then
		crawlAnim:AdjustSpeed(humanoid.WalkSpeed/8)
		if humanoid.MoveDirection ~= Vector3.new(0,0,0)then
			if crawlAnim.IsPlaying ~= true then
				crawlAnim:Play()
			end
		else
			crawlAnim:Stop()
			crawlAnimIdle:Play()
		end
	end
end

game:GetService("UserInputService").InputBegan:connect(onKeyPress)

That’s because your InputBegan event is connected after a non-terminating while loop, so the while loop is blocking it from being connected. Connect it before the event or wrap the loop in a pseudothread (via coroutine or spawn). Connecting before the event is more preferred.