Boolean turns true after being set to false

I hate it when im working on something and then a minor thing just throws off my motivation, i’m working on a horror game, i put a value where if it’s true you can crouch, but when i set it to false and i try to crouch it turns into true, i made it so i would print the value of the boolean when you crouch and it printed true after setting it to false.

I just want a fix for this thing. Thanks.

CrouchScript:

uis.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.LeftControl then
		print(CanCrouch)
		if bool == false and CanCrouch == true then
			canSprint = false
			print(CanCrouch)
			anim:Play()
			hum.WalkSpeed = 8
			hum.CameraOffset = Vector3.new(0, -0.4, -1.4)
			wait()
			bool = true
		end
	end
end)

SprintScript:

uis.InputBegan:Connect(function(key, processed)
	if key.KeyCode == Enum.KeyCode.LeftShift then
		if bool.Value == true and running == false and hum.MoveDirection.magnitude > 0 and stamina > 0 then
			running = true
			CanCrouch = false
			print(CanCrouch)
			sprint:Play()
			fov:Play()
			while running do
				task.wait(0.075)
				print(CanCrouch)
				print(stamina)
				bar.Size = UDim2.fromScale(stamina / maxStamina, 1)
				stamina -= 1
				frame.Visible = true
				if stamina == 0 then
					tired:Play()
					normal:Play()
					running = false
				end
			end 
		end
	end
end)
1 Like

You forgot to put .Value on the bools in the crouch script

In the variables i put .Value, so that’s not the problem

1 Like

Can we get more of the script? There are local variables you’re defining that we have no way of knowing where they lead.

1 Like

Sprint:

local char = script.Parent
local hum = char:WaitForChild("Humanoid")
local plr = game:GetService("Players").LocalPlayer
local frame = plr:WaitForChild("PlayerGui"):WaitForChild("Stamina"):WaitForChild("Background")

local bar = frame:WaitForChild("Bar")

local CanCrouch = char:WaitForChild("CrouchScript"):WaitForChild("CanCrouch").Value

local stamina2 = char:WaitForChild("Stamina")

local running = false
local bool = script:WaitForChild("CanSprint")

local uis = game:GetService("UserInputService")
local ts = game:GetService("TweenService")

local cam = workspace.CurrentCamera

local maxStamina = 100
local stamina = char:WaitForChild("Stamina").Value

local ti1 = TweenInfo.new(0.3, Enum.EasingStyle.Linear, Enum.EasingDirection.Out)
local ti2 = TweenInfo.new(0.3, Enum.EasingStyle.Linear, Enum.EasingDirection.In)

local sprint = ts:Create(hum, ti1, {WalkSpeed = 18})
local fov = ts:Create(cam, ti1, {FieldOfView = 72})

local walk = ts:Create(hum, ti2, {WalkSpeed = 12})
local normal = ts:Create(cam, ti2, {FieldOfView = 70})

local tired = ts:Create(hum, ti2, {WalkSpeed = 8})

bool.Value = true
stamina = maxStamina
frame.Visible = false

uis.InputBegan:Connect(function(key, processed)
	if key.KeyCode == Enum.KeyCode.LeftShift then
		if bool.Value == true and running == false and hum.MoveDirection.magnitude > 0 and stamina > 0 then
			running = true
			CanCrouch = false
			print(CanCrouch)
			sprint:Play()
			fov:Play()
			while running do
				task.wait(0.075)
				print(CanCrouch)
				print(stamina)
				bar.Size = UDim2.fromScale(stamina / maxStamina, 1)
				stamina -= 1
				frame.Visible = true
				if stamina == 0 then
					tired:Play()
					normal:Play()
					running = false
				end
			end 
		end
	end
end)

Crouch:

local char = script.Parent

local uis = game:GetService("UserInputService")

local hum = char:WaitForChild("Humanoid")
local animator = hum:WaitForChild("Animator")

local bool = script:WaitForChild("Crouching").Value

local CanCrouch = script:WaitForChild("CanCrouch").Value

local track = script:WaitForChild("Crouch")

local anim = animator:LoadAnimation(track)

local canSprint = char:WaitForChild("Sprint"):WaitForChild("CanSprint").Value

bool = false
CanCrouch = true

uis.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.LeftControl then
		print(CanCrouch)
		if bool == false and CanCrouch == true then
			canSprint = false
			print(CanCrouch)
			anim:Play()
			hum.WalkSpeed = 8
			hum.CameraOffset = Vector3.new(0, -0.4, -1.4)
			wait()
			bool = true
		end
	end
end)
1 Like

Okay, so you’re defining ‘bool’ in 2 different scripts, when you put it to false in one script, it won’t replicate to the other. I suggest creating an actual BoolValue item to work with instead of the script.

the boolValue is actually an instance look at the variables closely

The variable will never change since you used .Value on it in the variable itself. You’ll need to set the variable to just the BoolValue and use .Value on it each time you need the updated value of the BoolValue.

1 Like

My bad, it’s an easy mistake. On line 8 of the sprint code. You define ‘bool’ as “script:WaitForChild(“Crouching”).Value”. You have to remove the .value because everytime you call on it, it will return as true.

Fixed Code:

local char = script.Parent

local uis = game:GetService("UserInputService")

local hum = char:WaitForChild("Humanoid")
local animator = hum:WaitForChild("Animator")

local bool = script:WaitForChild("Crouching")

local CanCrouch = script:WaitForChild("CanCrouch").Value

local track = script:WaitForChild("Crouch")

local anim = animator:LoadAnimation(track)

local canSprint = char:WaitForChild("Sprint"):WaitForChild("CanSprint").Value

bool.Value = false
CanCrouch = true

uis.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.LeftControl then
		print(CanCrouch)
		if bool.Value == false and CanCrouch == true then
			canSprint = false
			print(CanCrouch)
			anim:Play()
			hum.WalkSpeed = 8
			hum.CameraOffset = Vector3.new(0, -0.4, -1.4)
			wait()
			bool.Value = true
		end
	end
end)
1 Like

OHHHHHHH, my god i feel so dumb, now i get it ;_;

2 Likes

You’re all good, it happens to the best of us lol. Glad we could help.

Thanks man, i appreciate it <3

1 Like