Weird bug in my code

I’ve got some weird bug and I’m not sure why

External Media
crouch script
local UserInputService = game:GetService("UserInputService")

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:wait()
local Humanoid = Character:WaitForChild("Humanoid")

local Animation = script:WaitForChild("Animation")
local Track = Humanoid:LoadAnimation(Animation)
Track.Looped = true
Track.Priority = Enum.AnimationPriority.Action
local runscript = script.Parent:WaitForChild("Sprintscript")

local activecrouch = false

local function CheckKeyDown(Key)
	if Key.KeyCode == Enum.KeyCode.LeftControl then
		if not activecrouch then
			activecrouch = true
			Track:Play()
			Humanoid.WalkSpeed = 8
			runscript.Enabled = false
			Humanoid.HipHeight = 1
			else
			
			activecrouch = false
			Track:Stop(print("stopped"))
			Humanoid.WalkSpeed = 16
			runscript.Enabled = true
			Humanoid.HipHeight = 2
			
		end
		
	end
end



UserInputService.InputBegan:Connect(CheckKeyDown)

sprint script
Uis = game:GetService("UserInputService")
local sprintspeed = 30 -- variable for sprint speed
local walkspeed = 16 -- variable for walk speed
local character = script.Parent -- variable for player
local hum = character:WaitForChild("Humanoid")
local Animator = hum:WaitForChild("Animator")
local camera = game.Workspace:FindFirstChild("Camera")

  -- Roblox dance emote


local SprintAnimation : AnimationTrack = Animator:LoadAnimation(script:WaitForChild("Animation"))
SprintAnimation.Priority = Enum.AnimationPriority.Movement

Uis.InputBegan:Connect(function(input,gameProcessedEvent) -- Event that sets walkspeed to 32 when holding shift key
	if gameProcessedEvent then return end
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.LeftShift then
			
		
			SprintAnimation:Play()
			character.Humanoid.WalkSpeed = 25
			camera.FieldOfView = 80
			
			repeat task.wait() until math.round(hum.MoveDirection.Magnitude) <= 0 
			SprintAnimation:Stop()
			hum.WalkSpeed = 16
			script.Parent.Crouch.Enabled = false
			
		end
	end
end)

Uis.InputEnded:Connect(function(input,gameProcessedEvent) -- Event that sets the original walkspeed when not holding shift key
	if gameProcessedEvent then return end
	if input.UserInputType == Enum.UserInputType.Keyboard then
		
		if input.KeyCode == Enum.KeyCode.LeftShift then
			SprintAnimation:Stop()
			character.Humanoid.WalkSpeed = 16 
			camera.FieldOfView = 70
			script.Parent.Crouch.Enabled = true
			
		end
	end
end)

sorry, i can’t view the streamable link; do you mind explaining the bug?

If I sprint and then crouch and I stop crouch the sprinting animation still plays

I tried to disable the crouch script when sprinting but it doesn’t do that for some reason

1 Like

after you stop crouching, did you release the shift key or still pressed onto it?

1 Like

I would be stoping the animations whenever left shift/ctrl isn’t pressed.

2 Likes

I was holding on to it (char limit)

1 Like

I did if you look at the script

1 Like

does it still occur when you release the key after you stopped crouching? cause that seems logical and not a bug?

1 Like

I fixed it I had misplaced the part where it stops the animation

2 Likes

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