Crouch system strange problems

Hello, I am trying to make a system where when you press C your character starts crouching.
However the problem is that the player will never stop crouching even tho the remoteEvent is fired to do so and the remoteEvent is fired immediately after C is pressed

local script, StarterPlayerScripts

local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local plr = game.Players.LocalPlayer
local remoteEvent = ReplicatedStorage:WaitForChild("MovementEvent") 
local reason = "Crouch"

local isCKeyPressed = true

local function onKeyPress(input, gameProcessedEvent)
	if input.KeyCode == Enum.KeyCode.C and not gameProcessedEvent then
		if isCKeyPressed == true then
			
			remoteEvent:FireServer(plr, reason)
		end
		task.wait(5)
		print("waiting")
		isCKeyPressed = false
	end
end

local function onKeyRelease(input, gameProcessedEvent)
	if input.KeyCode == Enum.KeyCode.C and not gameProcessedEvent then
		if isCKeyPressed == false then
			
			local endCrouch = true

			remoteEvent:FireServer(plr, endCrouch)
		end
		task.wait(5)
		print("waiting")
		isCKeyPressed = true
	end
end

UserInputService.InputBegan:Connect(onKeyPress)
UserInputService.InputEnded:Connect(onKeyRelease)

server script, ServerScriptService

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("MovementEvent")

local anim = Instance.new("Animation")
anim.AnimationId = "http://www.roblox.com/Asset?ID=15209290885"
local track

remoteEvent.OnServerEvent:Connect(function(plr, reason)
	
	if reason then
	   
		
	character = plr.Character
		
	if character then
			humanoid = character:FindFirstChild("Humanoid")
			
			if humanoid then
				print("lowering stats")
				
				humanoid.WalkSpeed = humanoid.WalkSpeed -10
			humanoid.JumpPower = 0
			track = humanoid:LoadAnimation(anim)
			track.Priority = Enum.AnimationPriority.Action
				track.Looped = true
				
				track:Play()
				
		end
		end
	else
		print("nuh uh")
		end
end)

remoteEvent.OnServerEvent:Connect(function(plr, endCrouch)
	
	if endCrouch then
		if track then
			print("stop track")
			track.Looped = false
			track:Stop()
			
		end
		
		if character then
			humanoid = character:FindFirstChild("Humanoid")

			if humanoid then
				print("change back")
				humanoid.WalkSpeed = humanoid.WalkSpeed +10
				humanoid.JumpPower = 45
				
			end
		end
	end

end)
2 Likes