Crouch Ctrl working but gui is currently not working!

Hello, another Dev Forum post! I am currently making a GUI crouch button with a ctrl input included. The one thing I’m having a problem is that I the button does not seem to work. The actual ctrl input works great just that the actual GUI does not work. It’s an image button but it does not seem to work at all. There are no error or messages in output and I’m really confused.

local humanoid = character:WaitForChild("Humanoid")
local crawlAnim = humanoid:LoadAnimation(script:WaitForChild("CrawlAnim"))
local isCrawling = false
local button = game.StarterGui.Crouch.ImageButton

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 = 8
	    humanoid.JumpPower = 0
		else
		crawlAnim:Stop()
		humanoid.WalkSpeed = 16
			    humanoid.JumpPower = 30
			isCrawling = false
		end
	end
end)



button.MouseButton1Down:Connect(function()
		if not isCrawling then
			isCrawling = true
			crawlAnim:Play()
			crawlAnim:AdjustSpeed(0)
			humanoid.WalkSpeed = 8
			humanoid.JumpPower = 0
		else
			crawlAnim:Stop()
			humanoid.WalkSpeed = 16
			humanoid.JumpPower = 30
			isCrawling = false
	end
end)

This script is located in StarterCharacterScripts and the crouch GUI is in StarterGuis. Any help is appreciated, thanks!

[EDIT]
Sorry, I wasnt checking DevForum. Pretty when you click the button, it puts the player in crouch and it works fine, but when you try to uncrouch the animation freezes and doesn’t stop. It keeps the same slow speed but does for some reason allow the player to jump. There are no errors in the output either so I don’t have any information from there. I hope this is enough information.

2 Likes

I don’t see where it changes or enables the gui…

You should add a 2nd variable in the inputbegan and inputended functions, it is called game processed event where it’ll indicate if the player is typing in chat or writing a text in a billboard gui, just a note.

Edit: btw, where’s the code lines where it makes the gui disabled or enabled?

Is it a local script, or a regular script? If it’s a regular script then I may have found your problem. All GUI things should be done in local scripts to function properly on the client side.

can you show us the script in the gui

Hey, you seem to be indexing the Gui from StarterGui:
local button = game.StarterGui.Crouch.ImageButton

When a player loads in, all the content in StarterGui is duplicated into PlayerGui, a child of the player.

You’d want to index the button with something like this:

local player = game.Players.LocalPlayer
local playerGui = player:WaitForChild(“PlayerGui”)

local crouchGui = playerGui:WaitForChild(“Crouch”)
local button = crouchGui:WaitForChild(“ImageButton”)

As everybody else said, you’re showing us what is working. Not what isn’t working! We need to see the issue, otherwise we cant help.

Sorry, I wasnt checking DevForum. Pretty when you click the button, it puts the player in crouch and it works fine, but when you try to uncrouch the animation freezes and doesn’t stop. It keeps the same slow speed but does for some reason allow the player to jump. There are no errors in the output either so I don’t have any information from there. I hope this is enough information.

Just figured it out. This helped but I also had another script inside of the button! Thank you!

1 Like