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

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)

This animation plays but in the next part of the script it never stops.

It somewhat works now, but It starts “T-Posing” meaning that the juggling animation doesn’t start instantly even though the Juggling variable is set to true.
I have also removed those occasional “Spasms” to the teddy bear for the moment because they seemed to bug out everything, also the reason for the while loop was that the “idle” animation isn’t looped, because I needed to insert those random “Spasms” (Also known as “Restless”) randomly, so the current code is:

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

local function AnimationHandler()
	if Juggling == true then
		a:Stop()
		e:Play()
	else
		--
		e:Stop()
		a:Play()
	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)

And here’s the video

Video

The Juggling starts as true

so you can call the animation to start animation this model, it would look like:

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

local function AnimationHandler()
	if Juggling == true then
		a:Stop()
		e:Play()
	else
		--
		e:Stop()
		a:Play()
	end
end
AnimationHandler()

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

this will probably start the game with the model animating

1 Like

It does! I’m not sure why though, isn’t the function supposed to run as soon as it shows up? like what’s the point of calling it and why does it work?

Here we create the function but the function will only run the code once you call it, after creating the function we call it, so as the Juggling variable is true already, it plays the animation

1 Like