Disable StarterCharacter script with keybind

What I want to achieve: Be able to disable my sprint script in the StarterCharacterScripts with the press of keycode LeftControl, I.E. when the player crouches, and RE enable the script with the press of the same button. This way I can prevent the player from being able to sprint while crouching

The Issue: I have created a crouch script that plays an animation and stops the animation when not moving, and it works completely fine, but I have zero Idea on how to implement a new function where I can have the same keybind disable the sprint script. (The crouch script is in StarterCharacterScripts as well.)
additional info: Iā€™m still relatively new to scripting and have a apprentice level knowledge of how Lua works. I am not asking that you completely write a new script for me or write the whole function for me, while appreciated, I would at the very minimum like an idea of where to start to get this idea to work.

My crouch script:

local UIS = game:GetService("UserInputService")

	local char = script.Parent
	local hum = char:WaitForChild("Humanoid")
	local animPlay = hum:LoadAnimation(script:WaitForChild("Animation"))
	local canCrawl = false

	hum.Running:Connect(function(speed)
		if speed > 0 then
			animPlay:AdjustSpeed(1)
		else
			animPlay:AdjustSpeed(0)
		end
	end)



	UIS.InputBegan:Connect(function(input)
		if input.KeyCode == Enum.KeyCode.LeftControl then
			if not canCrawl then
				canCrawl = true
				hum.HipHeight = 0.6
				hum.WalkSpeed = 8
				hum.JumpPower = 0
			animPlay:Play()
			else
				canCrawl = false
				hum.HipHeight = 2
				hum.WalkSpeed = 10
				hum.JumpPower = 50
			animPlay:Stop()
			end
		end
	end)
1 Like

I solved it! I created an entirely new script that I placed in StarterCharacterScripts
For those who want to the code

local SprintHandler = script.Parent:WaitForChild("SprintHandler")
local isDisabled = false

game:GetService("UserInputService").InputBegan:Connect(function(inputObject)
	if inputObject.KeyCode == Enum.KeyCode.LeftControl then
		if isDisabled then
			SprintHandler.Disabled = false
			isDisabled = false
		else
			SprintHandler.Disabled = true
			isDisabled = true
		end
	end
end)

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