Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How would I go about making my keybind crouch not execute while typing?

While typing my crouch script activates, any way around this as its annoying and needs to be fixed?

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

2 answers

1. Be consistent and localize all variables.

crawling is global, humanoid is local, crawlAnim is global, what??

2. The listeners of InputBegan get a second argument.

The second argument is a boolean. true if the input was caused by interaction with interface. false otherwise. That is, typing in a TextBox or clicking a GUI button.

local humanoid = script.Parent.Humanoid
local crawlAnim, crawlAnimIdle
local UserInputService = game:GetService("UserInputService")
local crawling = Instance.new("BoolValue")
crawling.Name = "crawling"
UserInputService.InputBegan:Connect(function(input, gameProcessed)
    if gameProcessed then
        return -- prematurely end the function if interacted with interface 
    end
    if input.KeyCode == Enum.KeyCode.C then
        if crawling.Value then -- == true not needed
            crawling.Value = false
            crawlAnim:Stop()
            crawlAnim:Destroy()
            crawlAnimIdle:Stop()
            crawlAnimIdle:Destroy()
            humanoid.WalkSpeed = humanoid.WalkSpeed*2
            humanoid.Parent.Head.Running.PlaybackSpeed = 1.85
        else
            crawling.Value = 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)

crawling.Changed:Connect(function(newValue)
    if newValue then
        crawlAnim:AdjustSpeed(humanoid.WalkSpeed/8)
        if humanoid.MoveDirection.Magnitude == 0 then
            -- ~= and == work on some roblox data types cuz of __eq
            -- so i prefer magnitude cuz numbers can be raw equal 
            if not crawlAnim.IsPlaying then
                crawlAnim:Play()
            end
        else
            crawlAnim:Stop()
            crawlAnimIdle:Play()
        end
    end
end)

Some changes I made:

  • crawling is now a reference to a BoolValue; that while loop was unacceptable. Calling wait in the conditional part is unacceptable too. You now got more efficient code. You're welcome. No more code unnecessarily executing every 1/30 seconds. Now only when the Value property changes.

  • Removed == true since you don't need it. The syntax of control structures are:

while expr do

end

repeat

until expr

if expr then

end

expr needs to be an expression. tables, userdatas, functions, threads, strings, booleans, numbers, nil, and function calls are all expressions.

2 comments

0

if not UIS:GetFocusedTextBox() then

0

This doesn't work? Any help?

This discussion is preserved as it was. New answers, comments, and votes are closed.