1 | local Bar = script.Parent |
2 | local Value = script.Parent.Parent.Enrg.Value |
3 | while true do |
4 | wait( 0.01 ) |
5 | Bar.Size = UDim 2. new(Value/ 100 , 0 , 1 , 0 ) |
6 |
7 | end |
When you start the game it changes the size to the desired size but when energy is changed it does not change with it. Can someone tell me why it doesnt change when value changes?
1 | local Bar = script.Parent |
2 | local Value = script.Parent.Parent.Enrg -- Removed the .Value |
3 | while true do |
4 | wait( 0.01 ) |
5 | Bar.Size = UDim 2. new(Value.Value/ 100 , 0 , 1 , 0 ) -- Added .Value to the Udim2 so the value would |
6 | -- be re counted every 0.01 second! |
7 |
8 | end |
The problem is, you are using .Value in variables, this not update the value in variable, for fix you need to get value in loop, not in variable.
Examples:
01 | --<<<< Remember to change the value with server >>>>-- |
02 |
03 | -- Updating |
04 |
05 | local MyValue = workspace.MValue.Value -- If change the value will not update, you need to update the value. |
06 | while true do |
07 | MyValue = workspace.MValue.Value -- Update variable to set new value |
08 | print (MyValue) |
09 | wait() |
10 | end |
11 |
12 | -- Updated |
13 | local MyValue = workspace.MValue -- here get the object of your value |
14 |
15 | while true do |
16 | -- Because the object is saved in the variable, you can get the updated value at any time. But you need to add MyValue.Value: |
17 | print (MyValue.Value) -- Print the updated value |
18 | wait() |
19 | end |
Its simple, only remove .Value from variable and put it on get the value:
1 | local Bar = script.Parent |
2 | local Value = script.Parent.Parent.Enrg -- Removed the .Value from here. for get the updated value |
3 | while true do |
4 | -- Changed wait location. |
5 | Bar.Size = UDim 2. new(Value.Value/ 100 , 0 , 1 , 0 ) -- Added .Value after get object to get the updated value |
6 | wait( 0.01 ) |
7 | end |
Also you can detect if value changed with Instance.Changed
Example:
1 | local obj = workspace.MyValue |
2 |
3 | obj.Changed:Connect( function () -- On change value start a function |
4 | print ( "Changed the Value of " .. tostring (obj.Name) .. "!" ) |
5 | end ) |
For your script is here:
1 | local Bar = script.Parent |
2 | local Value = script.Parent.Parent.Enrg -- Removed the .Value from here. for get the updated value |
3 | Value.Changed:Connect( function () -- Detect if value changed |
4 | Bar.Size = UDim 2. new(Value.Value/ 100 , 0 , 1 , 0 ) -- Added .Value after get object to get the updated value |
5 | end |