Can't crouch with a bool value check (the bool value it's checked to true)

Hi, I tried putting a bool check so if set to false cannot crouch, if set to true, can crouch.
But I cannot crouch even the bool value if it’s set to true or false

Here’s the crouch script

task.wait(1)
local TweenService = game:GetService("TweenService") 
local tweenPart = game.Players.LocalPlayer.Character.Humanoid 
local canCrouch = game.ReplicatedStorage.Values.canCrouch

local info = TweenInfo.new(
    0.325,           
    Enum.EasingStyle.Back,     
    Enum.EasingDirection.Out,    
    0,                   
    false,                 
    0                      
)

local Goals = {           
    CameraOffset = Vector3.new(0,-1.5,0), 
}
local info2 = TweenInfo.new(
    0.3,          
    Enum.EasingStyle.Back,     
    Enum.EasingDirection.Out,    
    0,                   
    false,                
    0                  
)

local Goals2 = {             
    CameraOffset = Vector3.new(0,0,0), 
}

local PartTween = TweenService:Create(tweenPart, info, Goals) 
local PartTween2 = TweenService:Create(tweenPart, info2, Goals2) 

local animidle = tweenPart:LoadAnimation(script.Parent.Idle)

local animwalk = tweenPart:LoadAnimation(script.Parent.Walk)

local crouching = false
local waiting = false

local userinputservice = game:GetService("UserInputService")
userinputservice.InputBegan:Connect(function(input)
	canCrouch:GetPropertyChangedSignal("Value"):Connect(function()
    if input.KeyCode == Enum.KeyCode.LeftControl then
        if waiting == false then
			if crouching == false then
				if canCrouch == true then
				script.Parent.ImageColor3 = Color3.new(1, 1, 1)
				script.Parent.Parent.ImageColor3 = Color3.new(0, 0.666667, 1)
                tweenPart.WalkSpeed = 8
                waiting = true
                crouching = true
                animidle:Play()
                PartTween:Play() --plays it
                tweenPart.Running:Connect(function(Speed)
                    if crouching == true then
                        if Speed >= 1 then
                            animidle:Stop()
                            if animwalk.IsPlaying == false then
                                animwalk:Play()
                            end

                        else
                            animidle:Play()
                            animwalk:Stop()
							end
					
                    end
                end)
                task.wait(0.05)
                waiting = false
            else
				script.Parent.ImageColor3 = Color3.new(0, 0, 0)
				script.Parent.Parent.ImageColor3 = Color3.fromRGB(135, 135, 135)
                tweenPart.WalkSpeed = 16
                waiting = true
                crouching = false
                animidle:Stop()
                animwalk:Stop()
                PartTween2:Play()
                task.wait(0.05)
                waiting = false
            end
        end

		end
	end
	
    if input.KeyCode == Enum.KeyCode.LeftShift then
        if waiting == false then
			if crouching == false then
				if canCrouch == true then
				script.Parent.ImageColor3 = Color3.new(1, 1, 1)
				script.Parent.Parent.ImageColor3 = Color3.new(0, 0.666667, 1)
                tweenPart.WalkSpeed = 8
                waiting = true
                crouching = true
                animidle:Play()
                PartTween:Play() --plays it
                tweenPart.Running:Connect(function(Speed)
                    if crouching == true then
                        if Speed >= 1 then
                            animidle:Stop()
                            if animwalk.IsPlaying == false then
                                animwalk:Play()
                            end

                        else
                            animidle:Play()
                            animwalk:Stop()
                        end
                    end
                end)
                task.wait(0.05)
                waiting = false
            else
				script.Parent.ImageColor3 = Color3.new(0, 0, 0)
				script.Parent.Parent.ImageColor3 = Color3.fromRGB(135, 135, 135)
                tweenPart.WalkSpeed = 16
                waiting = true
                crouching = false
                animidle:Stop()
                animwalk:Stop()
                PartTween2:Play()
                task.wait(0.05)
                waiting = false
            end
        end

		end
	end
	end)
	
end)

	
script.Parent.MouseButton1Click:Connect(function()
	canCrouch:GetPropertyChangedSignal("Value"):Connect(function()
    if waiting == false then
		if crouching == false then
			if canCrouch == true then
			script.Parent.ImageColor3 = Color3.new(1, 1, 1)
			script.Parent.Parent.ImageColor3 = Color3.new(0, 0.666667, 1)
            tweenPart.WalkSpeed = 8
            waiting = true
            crouching = true
            animidle:Play()
            PartTween:Play() --plays it
            tweenPart.Running:Connect(function(Speed)
                if crouching == true then
                    if Speed >= 1 then
                        animidle:Stop()
                        if animwalk.IsPlaying == false then
                            animwalk:Play()
                        end

                    else
                        animidle:Play()
                        animwalk:Stop()
                    end
                end
            end)
            task.wait(0.05)
            waiting = false
        else
			script.Parent.ImageColor3 = Color3.new(0, 0, 0)
			script.Parent.Parent.ImageColor3 = Color3.fromRGB(135, 135, 135)
            tweenPart.WalkSpeed = 16
            waiting = true
            crouching = false
            animidle:Stop()
            animwalk:Stop()
            PartTween2:Play()
            task.wait(0.05)
            waiting = false
			end
			
		end
		
		end
	end)
	
end)


canCrouch:GetPropertyChangedSignal("Value"):Connect(function()
if crouching == true and canCrouch == false then
	crouching = false
	end
	
end)

Help would be appreciated.

If you check output, is there any errors?

There isn’t???

Can you try putting some prints and printing the value?

Doesn’t print???

Try this

wait(1)
local TweenService = game:GetService("TweenService") 
local tweenPart = game.Players.LocalPlayer.Character.Humanoid

local info = TweenInfo.new(
    0.325, 
    Enum.EasingStyle.Back, 
    Enum.EasingDirection.Out, 
    0,            
    false,        
    0 
)

local Goals = {   
    CameraOffset = Vector3.new(0,-1.5,0),
}
local info2 = TweenInfo.new(
    0.3,    
    Enum.EasingStyle.Back, 
    Enum.EasingDirection.Out,  
    0,    
    false,        
    0     
)

local Goals2 = {  
    CameraOffset = Vector3.new(0,0,0),
}

local PartTween = TweenService:Create(tweenPart, info, Goals) 
local PartTween2 = TweenService:Create(tweenPart, info2, Goals2)

local animidle = tweenPart:LoadAnimation(script.Parent.Idle)

local animwalk = tweenPart:LoadAnimation(script.Parent.Walk)
local crouching = false
local waiting = false

local userinputservice = game:GetService("UserInputService")
userinputservice.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.LeftControl then
        if waiting == false then
            if crouching == false then
				script.Parent.ImageColor3 = Color3.new(1, 1, 1)
				script.Parent.Parent.ImageColor3 = Color3.new(0, 0.666667, 1)
                tweenPart.WalkSpeed = 8
                waiting = true
                crouching = true
                animidle:Play()
                PartTween:Play() --plays it
                tweenPart.Running:Connect(function(Speed)
                    if crouching == true then
                        if Speed >= 1 then
                            animidle:Stop()
                            if animwalk.IsPlaying == false then
                                animwalk:Play()
                            end

                        else
                            animidle:Play()
                            animwalk:Stop()
                        end
                    end
                end)
                wait(0.05)
                waiting = false
            else
				script.Parent.ImageColor3 = Color3.new(0, 0, 0)
				script.Parent.Parent.ImageColor3 = Color3.fromRGB(135, 135, 135)
                tweenPart.WalkSpeed = 16
                waiting = true
                crouching = false
                animidle:Stop()
                animwalk:Stop()
                PartTween2:Play()
                wait(0.05)
                waiting = false
            end
        end

    end
    if input.KeyCode == Enum.KeyCode.LeftShift then
        if waiting == false then
            if crouching == false then
				script.Parent.ImageColor3 = Color3.new(1, 1, 1)
				script.Parent.Parent.ImageColor3 = Color3.new(0, 0.666667, 1)
                tweenPart.WalkSpeed = 8
                waiting = true
                crouching = true
                animidle:Play()
                PartTween:Play() --plays it
                tweenPart.Running:Connect(function(Speed)
                    if crouching == true then
                        if Speed >= 1 then
                            animidle:Stop()
                            if animwalk.IsPlaying == false then
                                animwalk:Play()
                            end

                        else
                            animidle:Play()
                            animwalk:Stop()
                        end
                    end
                end)
                wait(0.05)
                waiting = false
            else
				script.Parent.ImageColor3 = Color3.new(0, 0, 0)
				script.Parent.Parent.ImageColor3 = Color3.fromRGB(135, 135, 135)
                tweenPart.WalkSpeed = 16
                waiting = true
                crouching = false
                animidle:Stop()
                animwalk:Stop()
                PartTween2:Play()
                wait(0.05)
                waiting = false
            end
        end

    end
end)
script.Parent.MouseButton1Click:Connect(function()
    if waiting == false then
        if crouching == false then
			script.Parent.ImageColor3 = Color3.new(1, 1, 1)
			script.Parent.Parent.ImageColor3 = Color3.new(0, 0.666667, 1)
            tweenPart.WalkSpeed = 8
            waiting = true
            crouching = true
            animidle:Play()
            PartTween:Play() --plays it
            tweenPart.Running:Connect(function(Speed)
                if crouching == true then
                    if Speed >= 1 then
                        animidle:Stop()
                        if animwalk.IsPlaying == false then
                            animwalk:Play()
                        end

                    else
                        animidle:Play()
                        animwalk:Stop()
                    end
                end
            end)
            wait(0.05)
            waiting = false
        else
			script.Parent.ImageColor3 = Color3.new(0, 0, 0)
			script.Parent.Parent.ImageColor3 = Color3.fromRGB(135, 135, 135)
            tweenPart.WalkSpeed = 16
            waiting = true
            crouching = false
            animidle:Stop()
            animwalk:Stop()
            PartTween2:Play()
            wait(0.05)
            waiting = false
        end
    end
end)
1 Like

This doesn’t use the canCrouch value but if wanted you can just test or check if canCrouch is true.

1 Like

I need to use canCrouch, since the sprint will break it.
So, i did check it true and doesn’t crouch do i need to put it somewhere else

But it somewhere else inside the script to detect it in the process. Maybe the beginning?

1 Like

its cuz ur detecting that if the bool value is set to true also when the button would be clicked it would update the script you get it the answer would be to remove the propery changed signal event

2 Likes

Removed the Property Changed Signal Event, but still doesn’t work?

show me current script

wait(1)
local TweenService = game:GetService("TweenService")
local tweenPart = game.Players.LocalPlayer.Character.Humanoid
local canCrouch = game.ReplicatedStorage.Values.canCrouch 

local info = TweenInfo.new(
    0.325,          
    Enum.EasingStyle.Back,     
    Enum.EasingDirection.Out,    
    0,                    
    false,                
    0                      
)

local Goals = {             
    CameraOffset = Vector3.new(0,-1.5,0), 
}
local info2 = TweenInfo.new(
    0.3,           
    Enum.EasingStyle.Back,     
    Enum.EasingDirection.Out,    
    0,                    
    false,                
    0                      
)

local Goals2 = {             
    CameraOffset = Vector3.new(0,0,0),
}

local PartTween = TweenService:Create(tweenPart, info, Goals) 
local PartTween2 = TweenService:Create(tweenPart, info2, Goals2) 

local animidle = tweenPart:LoadAnimation(script.Parent.Idle)

local animwalk = tweenPart:LoadAnimation(script.Parent.Walk)

local crouching = false
local waiting = false


local userinputservice = game:GetService("UserInputService")
userinputservice.InputBegan:Connect(function(input)
	if canCrouch == true then
    if input.KeyCode == Enum.KeyCode.LeftControl then
		if waiting == false then
			
				
				if crouching == false then
				
				script.Parent.ImageColor3 = Color3.new(1, 1, 1)
				script.Parent.Parent.ImageColor3 = Color3.new(0, 0.666667, 1)
                tweenPart.WalkSpeed = 8
                waiting = true
                crouching = true
                animidle:Play()
                PartTween:Play() --plays it
                tweenPart.Running:Connect(function(Speed)
                    if crouching == true then
                        if Speed >= 1 then
                            animidle:Stop()
                            if animwalk.IsPlaying == false then
                                animwalk:Play()
                            end

                        else
                            animidle:Play()
                            animwalk:Stop()
							end
					
                    end
                end)
                wait(0.05)
                waiting = false
            else
				script.Parent.ImageColor3 = Color3.new(0, 0, 0)
				script.Parent.Parent.ImageColor3 = Color3.fromRGB(135, 135, 135)
                tweenPart.WalkSpeed = 16
                waiting = true
                crouching = false
                animidle:Stop()
                animwalk:Stop()
                PartTween2:Play()
                wait(0.05)
                waiting = false
            end
        end

		end
	end
	
	if input.KeyCode == Enum.KeyCode.LeftShift then
		if canCrouch == true then
        if waiting == false then
			if crouching == false then
				
				script.Parent.ImageColor3 = Color3.new(1, 1, 1)
				script.Parent.Parent.ImageColor3 = Color3.new(0, 0.666667, 1)
                tweenPart.WalkSpeed = 8
                waiting = true
                crouching = true
                animidle:Play()
                PartTween:Play() --plays it
                tweenPart.Running:Connect(function(Speed)
                    if crouching == true then
                        if Speed >= 1 then
                            animidle:Stop()
                            if animwalk.IsPlaying == false then
                                animwalk:Play()
                            end

                        else
                            animidle:Play()
                            animwalk:Stop()
                        end
                    end
                end)
                wait(0.05)
                waiting = false
            else
				script.Parent.ImageColor3 = Color3.new(0, 0, 0)
				script.Parent.Parent.ImageColor3 = Color3.fromRGB(135, 135, 135)
                tweenPart.WalkSpeed = 16
                waiting = true
                crouching = false
                animidle:Stop()
                animwalk:Stop()
                PartTween2:Play()
                wait(0.05)
                waiting = false
            end
        end

		end
	end
	
end)
script.Parent.MouseButton1Click:Connect(function()
	if canCrouch == true then
    if waiting == false then
		if crouching == false then
			
			script.Parent.ImageColor3 = Color3.new(1, 1, 1)
			script.Parent.Parent.ImageColor3 = Color3.new(0, 0.666667, 1)
            tweenPart.WalkSpeed = 8
            waiting = true
            crouching = true
            animidle:Play()
            PartTween:Play() --plays it
            tweenPart.Running:Connect(function(Speed)
                if crouching == true then
                    if Speed >= 1 then
                        animidle:Stop()
                        if animwalk.IsPlaying == false then
                            animwalk:Play()
                        end

                    else
                        animidle:Play()
                        animwalk:Stop()
                    end
                end
            end)
            wait(0.05)
            waiting = false
        else
			script.Parent.ImageColor3 = Color3.new(0, 0, 0)
			script.Parent.Parent.ImageColor3 = Color3.fromRGB(135, 135, 135)
            tweenPart.WalkSpeed = 16
            waiting = true
            crouching = false
            animidle:Stop()
            animwalk:Stop()
            PartTween2:Play()
            wait(0.05)
            waiting = false
			end
			
		end
		
		end
end)



if crouching == true and canCrouch == false then
	crouching = false
end

make canCrouch a variable

I could do that but, I have a sprint (stamina) thingy. While I sprint crouching it’s gonna be too op. So I need to disable crouch while sprinting.

then just join the two scripts

1 Like

Doesn’t crouch, added warns.

task.wait(1)
local Bar = script.Parent.Parent.Stamina:WaitForChild("Border"):WaitForChild("Bar")
local player = game.Players.LocalPlayer

local walk = 14
local run = 24

local power = 10
local sprinting = false


local character = player.Character or player.CharacterAdded:Wait()

local Animator = character.Humanoid:FindFirstChild("Animator")
local Run = Animator:LoadAnimation(script.Run)


local TweenService = game:GetService("TweenService")
local tweenPart = game.Players.LocalPlayer.Character.Humanoid
local canCrouch = true

local info = TweenInfo.new(
    0.325,          
    Enum.EasingStyle.Back,     
    Enum.EasingDirection.Out,    
    0,                    
    false,                
    0                      
)

local Goals = {             
    CameraOffset = Vector3.new(0,-1.5,0), 
}
local info2 = TweenInfo.new(
    0.3,           
    Enum.EasingStyle.Back,     
    Enum.EasingDirection.Out,    
    0,                    
    false,                
    0                      
)

local Goals2 = {             
    CameraOffset = Vector3.new(0,0,0),
}

local PartTween = TweenService:Create(tweenPart, info, Goals) 
local PartTween2 = TweenService:Create(tweenPart, info2, Goals2) 

local animidle = tweenPart:LoadAnimation(script.Idle)

local animwalk = tweenPart:LoadAnimation(script.Walk)

local crouching = false
local waiting = false
local canSprint = true 


local Check = coroutine.wrap(function()	
	while wait(0.6) do
		if crouching == true and canCrouch == false then
			crouching = false
		end
	end

end)

local userinputservice = game:GetService("UserInputService")
userinputservice.InputBegan:Connect(function(input)
	if canCrouch == true then
    if input.KeyCode == Enum.KeyCode.LeftControl then
		if waiting == false then
			if canSprint == true then
				warn(canSprint, canCrouch)
				if crouching == false then
					sprinting = false
					canSprint = false
				script.Parent.Crouch.ImageButton.ImageColor3 = Color3.new(1, 1, 1)
				script.Parent.Crouch.ImageColor3 = Color3.new(0, 0.666667, 1)
                tweenPart.WalkSpeed = 8
                waiting = true
                crouching = true
                animidle:Play()
                PartTween:Play() --plays it
                tweenPart.Running:Connect(function(Speed)
                    if crouching == true then
                        if Speed >= 1 then
                            animidle:Stop()
                            if animwalk.IsPlaying == false then
                                animwalk:Play()
                            end

                        else
                            animidle:Play()
                            animwalk:Stop()
							end
					
                    end
                end)
                wait(0.05)
                waiting = false
					else
						warn(canSprint, canCrouch)
				script.Parent.Crouch.ImageButton.ImageColor3 = Color3.new(0, 0, 0)
				script.Parent.Crouch.ImageColor3 = Color3.fromRGB(135, 135, 135)
                tweenPart.WalkSpeed = 16
						waiting = true
						canSprint = true
                crouching = false
                animidle:Stop()
                animwalk:Stop()
                PartTween2:Play()
                wait(0.05)
                waiting = false
					end
					
				end
				
			end	
			
		end
	end
	
	if input.KeyCode == Enum.KeyCode.LeftShift then
		if canCrouch == true then
        if waiting == false then
				if crouching == false then
					if canSprint == true then
						warn(canSprint, canCrouch)
					canSprint = false
					sprinting = false
					script.Parent.Crouch.ImageButton.ImageColor3 = Color3.new(1, 1, 1)
					script.Parent.Crouch.ImageColor3 = Color3.new(0, 0.666667, 1)
                tweenPart.WalkSpeed = 8
                waiting = true
                crouching = true
                animidle:Play()
                PartTween:Play() --plays it
                tweenPart.Running:Connect(function(Speed)
                    if crouching == true then
                        if Speed >= 1 then
                            animidle:Stop()
                            if animwalk.IsPlaying == false then
                                animwalk:Play()
                            end

                        else
                            animidle:Play()
                            animwalk:Stop()
                        end
                    end
                end)
                wait(0.05)
                waiting = false
						
					else
						warn(canSprint, canCrouch)
						canSprint = false
				script.Parent.Crouch.ImageButton.ImageColor3 = Color3.new(0, 0, 0)
				script.Parent.Crouch.ImageColor3 = Color3.fromRGB(135, 135, 135)
                tweenPart.WalkSpeed = 16
                waiting = true
                crouching = false
                animidle:Stop()
                animwalk:Stop()
                PartTween2:Play()
                wait(0.05)
                waiting = false
            end
				end
			end
			

		end
	end
	
end)
script.Parent.Crouch.ImageButton.MouseButton1Click:Connect(function()
	if canCrouch == true then
    if waiting == false then
			if crouching == false then
				if canSprint == true then
					sprinting = false
					canSprint = false
					warn(canSprint, canCrouch)
				script.Parent.Crouch.ImageButton.ImageColor3 = Color3.new(1, 1, 1)
				script.Parent.Crouch.ImageColor3 = Color3.new(0, 0.666667, 1)
            tweenPart.WalkSpeed = 8
            waiting = true
            crouching = true
            animidle:Play()
            PartTween:Play() --plays it
            tweenPart.Running:Connect(function(Speed)
                if crouching == true then
                    if Speed >= 1 then
                        animidle:Stop()
                        if animwalk.IsPlaying == false then
                            animwalk:Play()
                        end

                    else
                        animidle:Play()
                        animwalk:Stop()
                    end
                end
            end)
            wait(0.05)
            waiting = false
				else
					warn(canSprint, canCrouch)
			script.Parent.Crouch.ImageButton.ImageColor3 = Color3.new(0, 0, 0)
			script.Parent.Crouch.ImageColor3 = Color3.fromRGB(135, 135, 135)
            tweenPart.WalkSpeed = 16
            waiting = true
            crouching = false
            animidle:Stop()
            animwalk:Stop()
            PartTween2:Play()
            wait(0.05)
            waiting = false
			end
			
		end
		
		end
	end
	
end)



Check()
-----------------------------



local TweenInformation = TweenInfo.new(
	0.6,
	Enum.EasingStyle.Quint,
	Enum.EasingDirection.InOut,
	0,
	false,
	0
)

--local tween = TweeningService:Create(camera, TweenInformation, FOVChanges)

local tween1 = TweenService:Create(script.Parent.Parent.Stamina:WaitForChild("Border"), TweenInformation, {BackgroundTransparency = 0})
local tween11 = TweenService:Create(Bar, TweenInformation, {BackgroundTransparency = 0})

local tween2 = TweenService:Create(script.Parent.Parent.Stamina:WaitForChild("Border"), TweenInformation, {BackgroundTransparency = 1})
local tween22 = TweenService:Create(Bar, TweenInformation, {BackgroundTransparency = 1})
userinputservice.InputBegan:Connect(function(key, gameProcessed)
	if key.KeyCode == Enum.KeyCode.LeftAlt and gameProcessed == false and canCrouch == true and canSprint == false then
		if character.Humanoid.Running then
			canSprint = true
			character.Humanoid.WalkSpeed = run
			canCrouch = false
			sprinting = true
			Run:Play()
			while power > 0 and sprinting do
				power = power - .06
				Bar.Size = UDim2.new(power / 10.2, 0, 0.638, 0)
				tween1:Play()
				tween11:Play()

				wait()
				if power <= 0.3 then
					character.Humanoid.WalkSpeed = walk
				end
			end

		end
	end
end)

userinputservice.InputEnded:Connect(function(key, gameProcessed)
	canSprint = false
	if key.KeyCode == Enum.KeyCode.LeftAlt and gameProcessed == false and canCrouch == false then
		character.Humanoid.WalkSpeed = walk
		canCrouch = true
		sprinting = false
		
		while power < 10 and not sprinting do
			power = power + .03
			tween2:Play()
			tween22:Play()
			Run:Stop()
			Bar.Size = UDim2.new(power / 10.2, 0, 0.638, 0)
			wait()
			if power <= 0.3 then
				Run:Stop()
				character.Humanoid.WalkSpeed = walk
			end
		end
	end
end)

this is cause ur detecting if the player is spring remove the canSprint