Values not changing?

So i’m making a battery that generates power, But the value in the battery doesn’t change at all.

local Power = script.Parent.PowerStored.Value
local MaxPower = 100

while true do
	if workspace.testgenerator.Active.Value == true then
		wait(1)
		if Power < MaxPower then
			Power = Power + 5
			print("Added 5 power")
			
		elseif Power > MaxPower then
			Power = MaxPower
			print("Full!")
		end
	end
	wait()
end

I have no clue why this happens it has happened before.

Edit: It changes in the script, But not the actual value inside the model.

1 Like

Try this

local Power = script.Parent.PowerStored
local MaxPower = 100
function test() 
while wait() do
	if workspace.testgenerator.Active.Value == true then
		wait(1)
		if Power.Value < MaxPower then
			Power.Value = Power.Value + 5
			print("Added 5 power")
			
		elseif Power.Value > MaxPower then
			Power.Value = MaxPower
			print("Full!")
		end
	end
end
end
test()
1 Like

I don’t know what you did but it worked, What did you do?

1 Like

Hello. When you declare the first variable, Power, you are asigning it a value, not a reference to a value. For example, let’s suppose that PowerStored’s value is 5. Now this statement:
local Power = script.Parent.PowerStored.Value
and this other one:
local Power = 5
are equivalent. As you see, Power is a constant, there is no reference to any object. Thus, any change made to the PowerStored value is not detected.

Try to asign the object to the variable instead, as @3Kropek just suggested.

3 Likes

Ohh, Now i see what i’ve done wrong, Thanks a lot!