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

Sneak Only While Crouching?

Im trying to make it so that when I crouch I can sneak. The problem is that when I stand the same code applied makes me sneak even while standing.

local UIS = game:GetService('UserInputService')
local Player = game.Players.LocalPlayer
local Character = Player.Character

PlayAnim = nil
	
UIS.InputBegan:connect(function(input, gameProcessedEvent)
	if input.KeyCode == Enum.KeyCode.W then 
		Character.Humanoid.WalkSpeed = 16
		local Anim = Instance.new('Animation')
		Anim.AnimationId = 'rbxassetid://04690454085'
		PlayAnim = Character.Humanoid:LoadAnimation(Anim)
		PlayAnim:Play()

	elseif input.KeyCode == Enum.KeyCode.C then 
	
	end
end)

UIS.InputEnded:connect(function(input, gameProcessedEvent)
	if input.KeyCode == Enum.KeyCode.W then
		PlayAnim:Stop()
	end
end)

The key to crouch is "C" so how do I say "When you press C that it will sneak and when you press C again then sneak is disabled"

???

1 comment

0

make a local variable called Crouching and set it to false (local Crouching = false) and then when the player crouches you check if Crouching is true, if not then set it to true and crouch, otherwise set it to false and uncrouch.

2 answers

✓ Accepted answer

We ended up solving it on discord. Took a while but we got it done. Here is the final result:

If you are reading this and have any question, post them below pls and I'll get back to u.

local UIS = game:GetService('UserInputService')
local Player = game.Players.LocalPlayer
local Character = Player.Character

local isCrouching = false
isOnCooldown = false
idleCrouchAnimation = nil
sneakAnimation = nil

--checks if the player is running (wasd keys + shift key)
function isRunning()
	if 
		UIS:IsKeyDown(Enum.KeyCode.LeftShift) and UIS:IsKeyDown(Enum.KeyCode.LeftShift)
		or UIS:IsKeyDown(Enum.KeyCode.W) and UIS:IsKeyDown(Enum.KeyCode.LeftShift)
		or UIS:IsKeyDown(Enum.KeyCode.A) and UIS:IsKeyDown(Enum.KeyCode.LeftShift)
		or UIS:IsKeyDown(Enum.KeyCode.S) and UIS:IsKeyDown(Enum.KeyCode.LeftShift)
		or UIS:IsKeyDown(Enum.KeyCode.D) and UIS:IsKeyDown(Enum.KeyCode.LeftShift)
	then
		return true 
	end
end

UIS.InputBegan:Connect(function(input, gameProcessedEvent)
	--if the player presses the crouch key, and theyre not on cooldown:
	if input.KeyCode == Enum.KeyCode.C and not isOnCooldown then
		isOnCooldown = true

		--if theyre running play the slide animation
		if isRunning() then
			--SLIDE ANIMATION 
		else 
			--if theyre not crouched, play crouch animation
			if not isCrouching then
				isCrouching = true
				Character.Humanoid.WalkSpeed = 6
	
				local animation = Instance.new('Animation')
				animation.AnimationId = 'rbxassetid://04690414995'
				idleCrouchAnimation = Character.Humanoid:LoadAnimation(animation)
	
				idleCrouchAnimation:Play()
				
			--if they are crouched, stop crouch animation
			else 
				isCrouching = false
				idleCrouchAnimation:Stop()
				Character.Humanoid.WalkSpeed = 16
			end
		end
		

		--wait .5 seconds and remove cooldown
		wait(.5)
		isOnCooldown = false
	
	--if theyre crouched and pressing w, play sneak animation
	elseif input.KeyCode == Enum.KeyCode.W and isCrouching then
		local animation = Instance.new('Animation')
		animation.AnimationId = 'rbxassetid://04690454085'
        sneakAnimation = Character.Humanoid:LoadAnimation(animation)
		sneakAnimation:Play()
	end
end)



--stop crouch idle when the player is running
UIS.InputBegan:Connect(function(input, gameProcessedEvent)
	print(isRunning(), sneakAnimation)
	if isRunning() and sneakAnimation  then
		sneakAnimation:Stop()
        	sneakAnimation = nil
	end

	if (input.KeyCode == Enum.KeyCode.LeftShift and UIS:IsKeyDown(Enum.KeyCode.W)) 
	or (input.KeyCode == Enum.KeyCode.W and UIS:IsKeyDown(Enum.KeyCode.LeftShift)) then
		idleCrouchAnimation:Stop()
	end
end)

--stop sneak animation when the player stops pressing w while sneaking
UIS.InputEnded:Connect(function(input, gameProcessedEvent)
    	if input.KeyCode == Enum.KeyCode.W and sneakAnimation then
        	sneakAnimation:Stop()
        	sneakAnimation = nil
	end
end)

Make a local variable called Crouching and set it to false (local Crouching = false) and then when the player crouches you check if Crouching is true, if not then set it to true and crouch, otherwise set it to false and uncrouch.

local Crouching = false
function Crouch()
	if Crouching == false then
		Crouching = true
		-- Crouch anim
	else
		Crouching = false
		-- Crouch anim
	end
end

1 comment

0

Do I put this inside of the crouching script or the sneak script? I dont understand completely.

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