Crawling script glitches when it's disabled mid-crawling

Hey there! I was facing this issue where this crawling script that I made would get enabled while typing in the chat so whenever anyone typed anything in the chat with the letter c in it, they would crawl. So I tried fixing it by making another script that disables the crawling script when the player starts typing and it did fix it. However, another issue resurfaced where if you type while crawling it starts glitching and you can not stop crawling at all.

The crawling script:

local UIS = game:GetService("UserInputService")
local char = script.Parent
local hum = char:WaitForChild("Humanoid")
local anim = hum:LoadAnimation(script:WaitForChild("Animation"))
local isCrawling = false
local STS = char.ShiftToSprint

--The Modified Part:
hum.Running:Connect(function(speed)
	if speed > 0 then
		anim:AdjustSpeed(1)
	else
		anim:AdjustSpeed(0)
	end
end)


UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.C then
		if not isCrawling then
			hum.WalkSpeed = 4
			isCrawling = true
			anim:Play()
            STS.Enabled = false
		else
			hum.WalkSpeed = 10
			anim:Stop()
			isCrawling = false
			STS.Enabled = true
		end
	end
end)

The anti-crawling script:

local scriptToDisable = script.Parent.Crawl
local typing = false -- Tracks whether the player is currently typing

local function enableScript()
	scriptToDisable.Disabled = false
end

local function disableScript()
	scriptToDisable.Disabled = true
end

game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(message)
		typing = true
		disableScript()
	end)
	player.Chatted:Connect(function(message)
		typing = false
		enableScript()
	end)
end)

local userInputService = game:GetService("UserInputService")

userInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if not gameProcessedEvent and not typing then
		enableScript()
	elseif not gameProcessedEvent and typing then
		disableScript()
	end
end)

userInputService.TextBoxFocused:Connect(function()
	typing = true
	disableScript()
end)

userInputService.TextBoxFocusReleased:Connect(function()
	typing = false
	enableScript()
end)

A video:

1 Like

You can add a check in the anti-crawling script to make sure that the crawling script is not currently active before disabling it.

local function enableScript()
	if not scriptToDisable.Parent then return end -- check if script is still in the character
	if not scriptToDisable.Disabled then return end -- check if script is already enabled
	scriptToDisable.Disabled = false
end

local function disableScript()
	if not scriptToDisable.Parent then return end -- check if script is still in the character
	if scriptToDisable.Disabled then return end -- check if script is already disabled
	scriptToDisable.Disabled = true
	local hum = scriptToDisable.Parent.Humanoid
	if hum.WalkSpeed == 4 then -- reset walk speed and animation if crawling
		hum.WalkSpeed = 10
		scriptToDisable.Animation:Stop()
		scriptToDisable.isCrawling = false
		scriptToDisable.ShiftToSprint.Enabled = true
	end
end

I tested it but I’m still facing the issue. It should look something like this right?

local scriptToDisable = script.Parent.Crawl
local typing = false -- Tracks whether the player is currently typing

local function enableScript()
	if not scriptToDisable.Parent then return end -- check if script is still in the character
	if not scriptToDisable.Disabled then return end -- check if script is already enabled
	scriptToDisable.Disabled = false
end

local function disableScript()
	if not scriptToDisable.Parent then return end -- check if script is still in the character
	if scriptToDisable.Disabled then return end -- check if script is already disabled
	scriptToDisable.Disabled = true
	local hum = scriptToDisable.Parent.Humanoid
	if hum.WalkSpeed == 4 then -- reset walk speed and animation if crawling
		hum.WalkSpeed = 10
		scriptToDisable.Animation:Stop()
		scriptToDisable.isCrawling = false
		scriptToDisable.Enabled = true
	end
end

game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(message)
		typing = true
		disableScript()
	end)
	player.Chatted:Connect(function(message)
		typing = false
		enableScript()
	end)
end)

local userInputService = game:GetService("UserInputService")

userInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if not gameProcessedEvent and not typing then
		enableScript()
	elseif not gameProcessedEvent and typing then
		disableScript()
	end
end)



userInputService.TextBoxFocused:Connect(function()
	typing = true
	disableScript()
end)

userInputService.TextBoxFocusReleased:Connect(function()
	typing = false
	enableScript()
end)

I managed to fix it.

local scriptToDisable = script.Parent.Crawl
local typing = false -- Tracks whether the player is currently typing
local character = script.Parent
local Animation = script.Parent.Crawl.Animation
local animtracks = character.Humanoid:GetPlayingAnimationTracks()

local function enableScript()
	if not scriptToDisable.Parent then return end -- check if script is still in the character
	if not scriptToDisable.Disabled then return end -- check if script is already enabled
	scriptToDisable.Disabled = false
end

local function disableScript()
	if not scriptToDisable.Parent then return end -- check if script is still in the character
	if scriptToDisable.Disabled then return end -- check if script is already disabled
	scriptToDisable.Disabled = true
	local hum = scriptToDisable.Parent.Humanoid
	if hum.WalkSpeed == 4 then -- reset walk speed and animation if crawling
		hum.WalkSpeed = 10
		for _, AnimationTrack in pairs(character.Humanoid:GetPlayingAnimationTracks()) do
			if AnimationTrack.Name == Animation.Name then
				AnimationTrack:Stop()
				break -- Break the loop, we already found and stopped the specified animation.
			end
		end
		scriptToDisable.Disabled = true
	end
end

game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(message)
		typing = true
		disableScript()
	end)
	player.Chatted:Connect(function(message)
		typing = false
		enableScript()
	end)
end)

local userInputService = game:GetService("UserInputService")

userInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if not gameProcessedEvent and not typing then
		enableScript()
	elseif not gameProcessedEvent and typing then
		disableScript()
	end
end)



userInputService.TextBoxFocused:Connect(function()
	typing = true
	disableScript()
end)

userInputService.TextBoxFocusReleased:Connect(function()
	typing = false
	enableScript()
end)

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