Change Crouch Keyboard Key to TextButton?

Hi everyone I’m currently working on a Crouch System and rn I just need to change the Keybind function to if the Textbutton which is the script Parent is pressed!

local keybind = "C"
local animationId = "17667816899"  

--// Variables
local UserInputService = game:GetService("UserInputService")

local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

local animation = Instance.new("Animation")
animation.AnimationId = 'rbxassetid://' .. animationId

local animationTrack = humanoid:WaitForChild("Animator"):LoadAnimation(animation)
animationTrack.Priority = Enum.AnimationPriority.Action

local crouching = false


--// Functions
UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if gameProcessedEvent then return end

	if input.KeyCode == Enum.KeyCode[keybind] then
		if crouching then
			humanoid.WalkSpeed = 16
			crouching = false
			animationTrack:Stop()
			character.HumanoidRootPart.CanCollide =  true
		else
			humanoid.WalkSpeed = 8
			crouching = true
			animationTrack:Play()
			character.HumanoidRootPart.CanCollide =  false
		end
	end

	while wait() do


		if humanoid.MoveDirection.Magnitude > 0 then


			animationTrack:AdjustSpeed(1) 


		else


			animationTrack:AdjustSpeed(0)


		end
	end

end)
1 Like

You can just connect both a keybind input and button press to one toggler, instead of connecting it directly to the keybind.

local function toggle()
    --toggler code here
end

local function processInput()
    --input code here
    task.spawn(toggle)
end

UserInputService.InputBegan:Connect(processInput)
script.Parent.MouseButton1Click:Connect(toggle)
2 Likes

I would suggest using context action service, it can help you and automatically create touch buttons that do the same things as the keyboard inputs

local contextactionservice = game:GetService("ContextActionService")

contextactionservice:BindAction("Crouch", function(Action : string, InputState : Enum.UserInputState)
	if (InputState == Enum.UserInputState.Begin) then
		print("crouch!")
	elseif (InputState == Enum.UserInputState.End) then
		print("stand!")
	end
end, true, Enum.KeyCode.LeftControl)
1 Like

This is why I made another topic:

I didn’t rlly understand the replies on the first topic and I thought it will be annoying if I ask again…but I mean I can mark ur answer on the first topic as Solution if u want

idk, if you don’t understand you should just ask! We are all here to learn. Personally, I think @AMisspelledUsernaem 's solution would be better, but they both should work!

1 Like

my bad I’m really sry I’m not gonna do it again

1 Like

Yoo I’m responding late and I believe you thought I I know how to do this but I actually don’t um… can pls tell me where exactly I have to put ur code or the code from the other guy?

Where you connect a function to the input began event you need to change.

--this is your function that toggles.
local function toggler()
    --you can toggle your crouch here.
end

--just manage the input here and call the function to toggle.
UserInputService.InputBegan:Connect(function(input, processed)
    --blah blah input stuff

    toggle()
end)

--now, hook your button.
script.Parent.MouseButton1Click:Connect(toggle)

This is how to implement mine - it is simpler, so would be easier for someone less experienced like you. Feel free to ask any questions!

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