How would I check if a bool variable parented to a local script is true from a different local script?

I am making a movement system, and I want all my animations to be played in one local script. Problem is, I can’t seem to find a way to check if a bool variable is true or false from the local script which handles all animations. I have tried using Boolean Attributes and Boolean Values but they would never seem to change from false to true at all, even though it was working from my crouch script. The Boolean Value was parented to the crouch script and so was the Attribute.

Any help is appreciated, Thanks!

1 Like

So a local script does everything on the client. So if you change that BoolValue on the client (using a localscript), that value will not be replicated to the other clients.

I think this is your question? Else I don’t see a problem.

1 Like

I know about values not being changed on a local script but I read a post saying Boolean Values can be changed if it is parented to something like a tool, or the character. Anyways all I am trying to do is make the value actually show the change so I can have an animation played.

1 Like

Are you sure? This works fine.

local BoolValue = Instance.new("BoolValue", script.Parent) -- In the LocalPlayer.

BoolValue.Value = true

print(BoolValue.Value) -- Make sure you print the Value property, it is a common mistake.

Could you share relevant code, or debug it?

1 Like

This is my Crouch Script:

--// Services

local ContextActionService = game:GetService("ContextActionService")

--// Variables

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

local CrouchSpeed = 8
local NormalSpeed = 16

local CrouchJump = 0
local NormalJump = 50

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

--// Functions

local function Handler(Name, State, Object)
	
	if Name == "Crouch" then
		
		if State == Enum.UserInputState.Begin then
			
			if Crouching == false then
				
				Humanoid.WalkSpeed = CrouchSpeed
				Humanoid.JumpPower = CrouchJump
				
				Crouching = true
			
			else
				
				Humanoid.WalkSpeed = NormalSpeed
				Humanoid.JumpPower = NormalJump

				Crouching = false
				
			end
			
		end
		
	end
	
end

--// Binding

ContextActionService:BindAction("Crouch", Handler, true, Enum.KeyCode.C)

This is my Animation Handler Script:

--// Variables

local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()

local Script = Character:WaitForChild("MovementScripts", math.huge):WaitForChild("ToggledCrouch", math.huge)
local BoolValue = Script:WaitForChild("Crouching", math.huge)

while true do
	
	print(BoolValue.Value)
	
	task.wait(1)
	
end

The Crouch Script works as intended but the animation handler script doesn’t show the Boolean Value being changed.

1 Like

You’re calling this wrong or the bool is in a place that is the sever not the client. Hard to say as you show no examples or file hierarchy. The other thing that comes to mind is you may not be waiting long enough for the bool to be ready. Try adding a local bool = where ever it is:WaitforChild(“the bool”)

The boolean is parented to the crouch script

Probably not yield for infinity? Remove that, so it is on its default, and maybe shows us the problem.

What ever this comes out to be it will not be a bool. True or False is a bool. Not a number.

The animation handler gets the bool value but will constantly print “False” even though the crouch script changes the value to true

the math.huge is the time out number which is how long it will wait before moving on. this is not a problem at all.

I see the problem.

local Crouching = script:WaitForChild("Crouching").Value, this is either true, or false. So a bool. Crouching = true, will make the boolean true, but not the Value property of the BoolValue.

Doesn’t mean it is totally there yet. Try adding a:
local humanoid = Character:WaitForChild(“Humanoid”)
Just to make sure the player is totally loaded …

Ok so for some reason removing the .value on the variable and putting it before the = signs when changing the value works, idk why but ty

1 Like

Can’t believe I missed that. Glad you found it.

So what you did is you signed variable Crouching to the Value property from the BoolValue. That Value from the BoolValue is a boolean, either true or false. So what you are doing is you are sigining Crouching to a boolean, as Value is a boolean.

Just for those who might have the same issue, here is a code example of how I got it to work:

local BoolValue = script:WaitForChild("BoolValue")

if BoolValue.Value == true then
	
	print("True")
	
else
	
	print("False")
	
end

Basically I just removed the “.Value” when referencing the Boolean Value.

Yeah, so Value is the property of the BoolValue, like Name is a property of a BoolValue as well. When you do BoolValue.Value you use that value.

local Value = BoolValue.Value -- true or false.
local Name = BoolValue.Name -- The name of the BoolValue.

print(Value) -- true or false.
print(BoolValue) -- The BoolValue.

if BoolValue then
     -- The BoolValue exists.
end

if BoolValue.Value then
     -- The BoolValue's Value property is true.
end

That is exactly why you use the documentation for everything.

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