Crouch key to button

I want this code to be converted so it can work in a textbutton, thanks:

local UserInputService = game:GetService("UserInputService")
local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
local crawlAnim = humanoid:LoadAnimation(script:WaitForChild("CrawlAnim"))
local isCrawling = false

humanoid.Running:Connect(function(speed)
	if speed > 0 then
		crawlAnim:AdjustSpeed(1)
	else
		crawlAnim:AdjustSpeed(0)
	end
end)
	
UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftControl then
		if not isCrawling then
			isCrawling = true
			crawlAnim:Play()
			crawlAnim:AdjustSpeed(0)
	        humanoid.WalkSpeed = 12
			humanoid.JumpPower = 0
		else
			crawlAnim:Stop()
			humanoid.WalkSpeed = 16
			humanoid.JumpPower = 50
			isCrawling = false
			end
	end
end)

Hierarchy:
image

I think you can do this (that check if the player moving) :

local Humanoid = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")
if Humanoid.MoveDirection.Magnitude > 0 then
    --your code if the player is moving--
end

And I recommend you to set that if the player is crouch :

Humanoid.JumpHeight = 0
--and if the player not crouch--
Humanoid.JumpHeight = --your jump height--

I hope that was helpful ! :yum:

1 Like

I changed the post to see if this way someone can solve it for me

1 Like

Appreciate the reply but it didnt work, still thanks tho

1 Like

What didn’t work ? :face_with_raised_eyebrow::thinking:
I need to know for help you … :neutral_face:

It didnt help me to solve the problem, still like i said i appreciate the reply, thanks

1 Like

So do that :

local Humanoid = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")
while wait() do
    if Humanoid.MoveDirection.Magnitude > 0 then
        print("Moving")
    else
        print("Not moving")
    end
end

Can I see the button hierarchy ? :grinning:

image

1 Like

I’ll go back later I’ll help you don’t worry !

Ill go to sleep i hope that when i wake up someone solves this for me

Instead of

UserInputService.InputBegan:Connect(function(input)

you should put

script.Parent.MouseButton1Click:Connect(function()

You can also remove the second line of that function which checks the input KeyCode, as it won’t be necessary anymore.

1 Like

if you want a key and a button maybe using the ContextActionService might be better?

local contextActionService = game:GetService("ContextActionService")

local function Crouch(actionName, inputState, inputObject)
    -- if the inputState is not begin exit the function
    if inputState ~= Enum.UserInputState.Begin then return end

    -- toggle crawling
    if isCrawling == false then
        isCrawling = true
        crawlAnim:Play()
        crawlAnim:AdjustSpeed(0)
        humanoid.WalkSpeed = 12
        humanoid.JumpPower = 0
    else
        isCrawling = false
        crawlAnim:Stop()
        humanoid.WalkSpeed = 16
        humanoid.JumpPower = 50
    end
end

-- create the action and setup the buttons image title and position
contextActionService:BindAction("Crouch", Crouch, true, Enum.KeyCode.LeftControl)
contextActionService:SetImage("Crouch", "rbxassetid://1826746856")
contextActionService:SetTitle("Crouch", "Crouch")
contextActionService:SetPosition("Crouch", UDim2.new(0.5, 0, 0.5, 0))
1 Like

Didnt work, causes errors, still appreciated tho

I would prefer a textbutton to do the job plus the animation doesnt continue when the character starts to move, still appreciated tho

Ok bro ur script only needed to be modified a bit for it to do what i want i “fixed” it but would really appreciate if you modify it to be a textbutton instead, heres the modified version of ur script:

local contextActionService = game:GetService("ContextActionService")
local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
local crawlAnim = humanoid:LoadAnimation(script:WaitForChild("CrawlAnim"))
local isCrawling = false
local function Crouch(actionName, inputState, inputObject)
	
	humanoid.Running:Connect(function(speed)
		if speed > 0 then
			crawlAnim:AdjustSpeed(1)
		else
			crawlAnim:AdjustSpeed(0)
		end
	end)
	-- if the inputState is not begin exit the function
	if inputState ~= Enum.UserInputState.Begin then return end
	


	-- toggle crawling
	
	if isCrawling == false then
		isCrawling = true
		crawlAnim:Play()
		crawlAnim:AdjustSpeed(0)
		humanoid.WalkSpeed = 12
		humanoid.JumpPower = 0
	else
		isCrawling = false
		crawlAnim:Stop()
		humanoid.WalkSpeed = 16
		humanoid.JumpPower = 50
	end
end

-- create the action and setup the buttons image title and position
contextActionService:BindAction("Crouch", Crouch, true, Enum.KeyCode.LeftControl)
contextActionService:SetImage("Crouch", "rbxassetid://1826746856")
contextActionService:SetTitle("Crouch", "Crouch")
contextActionService:SetPosition("Crouch", UDim2.new(0.5, 0, 0.5, 0))

i will be waiting for ur response :slight_smile:

You should not connect to the running event everytime the function is called you should only connect to it once outside the function

1 Like

Ok, i can do that but can you modify the script to work with a textbutton? thanks :slight_smile:

Can someone solve this for me? thanks :sweat_smile:

Maybe this article will help you

You need to use this event to detect when the button is pressed

https://developer.roblox.com/en-us/api-reference/event/GuiButton/Activated

There is some example code on the first link