Script doesn't seem to check Bool Value [solved]

Hello, I’m trying to make it so that when you Turn off a light generator various things happen, I’m not using a single script though, so I needed a way to make them communicate somehow, because of this I used a module script for simplicity and a bool value to make something specific happen - if that makes sense.

Basically, everything works just fine, the module script changes the value as intended and all the other things that should be happening happen

Like shown here

The thing is that when I try to check if that bool value is true or false in another script, it just doesn’t work.
Here’s the first part of the script.

Juggling = true
local hum = script.Parent:WaitForChild("Humanoid")
local a = hum:LoadAnimation(script.Idle)
local b = hum:LoadAnimation(script.Restless1)
local c = hum:LoadAnimation(script.Restless2)
local d = hum:LoadAnimation(script.Restless3)
local e = hum:LoadAnimation(script.Juggling1)
local GenValue = script.GeneratorOn

if GenValue.Value == false then
	print("Gen Value Changed")
end

if Juggling == true then
	e:Play()
end

The “Juggling” animation works so I guess it just skips the print statement?

I’m as new as you can be to scripting so I might be doing something wrong here, I just don’t know what.
The bool value is inside the script above.

It checks only once so you need to check it again and again in a loop. Just change this to

while task.wait(0.75) do
	if GenValue.Value == false then
		print("Gen Value Changed")
	end
end
1 Like

What I wanted to do though is that once the bool value becomes false then Juggling = false and when the bool value becomes true again then Juggling = true
but if I make it:

while task.wait(0.75) do
	if GenValue.Value == false then
		Juggling = false
	end
end

The juggling animation stops as soon as I start the game even though the value is set to True as default

Just do it like this then?

while task.wait(0.75) do
	Juggling = GenValue.Value
end
1 Like

What you can do is to use the event Changed to check when the Value changes, so you can do your code from there.

local Bool = script.Bool

Bool.Changed:Connect(function(NewValue)
    if NewValue then -- Value = true

    else -- Value = false

    end
end)
1 Like

I think it would be better to use a .Changed event instead of a loop less costly

GenValue.Changed:Connect(function()
-- Set Bool
end)
1 Like

Okay, I’ll agree with @RaFaeL_LRFL. Because instead of checking every 0.75 seconds just check when the value changed.

Also do this for a smaller code.

Bool.Changed:Connect(function(NewValue)
    Juggling = NewValue
end)
1 Like

Okay one, please don’t do a loop, second put prints almost everywhere in your script, as you might have an infinite wait for child:

local hum = script.Parent:WaitForChild("Humanoid")

Third, is the value changed by a localscript, or a server script?

Also, use GetPropertyChangedSignal, since you only check the value once.

GenValue:GetProperyChangedSignal("Value"):Connect(function()
   if GenValue.Value == false then
	print("Gen Value Changed")
   end
end)
1 Like

The value is changed by a server script by a module script

1 Like

I will try what you guys said and I’ll let you know :smiley:

Almost forgot to mention. Humanoid:LoadAnimation() is already deprecated. Use Humanoid:WaitForChild("Animator"):LoadAnimation(YourAnimation) instead.

1 Like

Okay, so loops are plainly inefficient, and there’s no point in doing them, as there are several other methods. GetPropertyChangedSignal (Instance | Roblox Creator Documentation) does not produce lag, and it’s a simple event/function (kind of. It monitors a simple property of an instance and when it’s changed it fires an event.). Also @AridFights1 said that Humanoid:LoadAnimation() is deprecated, which is true, Animator is the child of Humanoid, which is a very simple change. So just change everything like Arid Said above.

2 Likes

As said I’m as new as you can be to scripting (and probably a bit dumb too) what am I supposed to change “NewValue” with?

The “NewValue” is the value that is in the BoolValue, like, if you change the BoolValue to “true”, then this event will return to you the new value that is “true” now

1 Like

instead of check “BoolValue.Value”, you already have this value

1 Like

Thanks, it now detects when a change happens to the value

Unfortunately, it doesn’t change the animation, which is a separate problem I guess, but I’m not sure why.

Juggling = true
local hum = script.Parent:WaitForChild("Humanoid")
local a = hum:WaitForChild("Animator"):LoadAnimation(script.Idle)
local b = hum:WaitForChild("Animator"):LoadAnimation(script.Restless1)
local c = hum:WaitForChild("Animator"):LoadAnimation(script.Restless2)
local d = hum:WaitForChild("Animator"):LoadAnimation(script.Restless3)
local e = hum:WaitForChild("Animator"):LoadAnimation(script.Juggling1)
local GenValue = script.GeneratorOn

GenValue.Changed:Connect(function(NewValue)
	if NewValue then
		Juggling = true
		print("Juggling is true")
	else
		Juggling = false
		print("Juggling is false")
	end
end)

if Juggling == true then
	e:Play()
end

while Juggling == false do

	local creepyIdle = true

	while creepyIdle==true do
		a:Play()
		wait(1.5)
		local x = math.random(1,3)
		if x==3 then
			creepyIdle = false
		end
	end

	local z = math.random(1,3)

	if z==1 then
		b:Play()
	elseif z==2 then
		c:Play()
	elseif z==3 then
		d:Play()
	end

	wait(0.2)
end

You forgot to stop the earlier animations…

earlier animations? what do you mean by that

Oh nvm I forgot juggling was a loop animation, but shouldn’t starting a new animation override the current one? Edit: If I stop the juggling animation on “Juggling == false” the other one doesn’t play anyway, and now the teddy is just T-Posing xP

In the while loop, it will only run the code while the Juggling is false, once it’s true, the loop will stop and won’t run again later, what you can do is to manage the animations when the value is changed. You can do too another function to manage the animations, like:

local function AnimationHandler()
    if Juggling == true then
    -- Play one animation
    else
    -- Play another animation
    end
end

GenValue.Changed:Connect(function(NewValue)
	if NewValue then
		Juggling = true
		print("Juggling is true")
	else
		Juggling = false
		print("Juggling is false")
	end
    AnimationHandler()
end)