Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How come this script does not constantly change the size of the bar?

Asked by 6 years ago
Edited 6 years ago
1local Bar = script.Parent
2local Value = script.Parent.Parent.Enrg.Value
3while true do
4    wait(0.01)
5        Bar.Size = UDim2.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
maybe this will help??: change Value to script.Parent.Parent.Enrg and line 5 to UDim2.new(Value.Value/100,0,1,0) greatneil80 2647 — 6y
0
Thank you!!! XD tightanfall 110 — 6y
0
As a note, using a while loop here is pretty inefficient. You may want to consider using the .Changed event instead. API link: https://developer.roblox.com/api-reference/event/NumberValue/Changed saenae 318 — 6y

2 answers

Log in to vote
0
Answered by 6 years ago
1local Bar = script.Parent
2local Value = script.Parent.Parent.Enrg -- Removed the .Value
3while true do
4    wait(0.01)
5        Bar.Size = UDim2.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
Ad
Log in to vote
0
Answered by
yHasteeD 1819 Moderation Voter
6 years ago
Edited 6 years ago

Problem

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 
05local MyValue = workspace.MValue.Value -- If change the value will not update, you need to update the value.
06while true do
07    MyValue = workspace.MValue.Value -- Update variable to set new value
08    print(MyValue)
09    wait()
10end
11 
12-- Updated
13local MyValue = workspace.MValue -- here get the object of your value
14 
15while 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()
19end

Correction

Its simple, only remove .Value from variable and put it on get the value:

1local Bar = script.Parent
2local Value = script.Parent.Parent.Enrg -- Removed the .Value from here. for get the updated value
3while true do
4    -- Changed wait location.
5    Bar.Size = UDim2.new(Value.Value/100,0,1,0) -- Added .Value after get object to get the updated value
6    wait(0.01)
7end

Also you can detect if value changed with Instance.Changed

  • This simple detect if Anything in object changed.

Example:

1local obj = workspace.MyValue
2 
3obj.Changed:Connect(function() -- On change value start a function
4    print("Changed the Value of " .. tostring(obj.Name) .. "!")
5end)

For your script is here:

1local Bar = script.Parent
2local Value = script.Parent.Parent.Enrg -- Removed the .Value from here. for get the updated value
3Value.Changed:Connect(function() -- Detect if value changed
4    Bar.Size = UDim2.new(Value.Value/100,0,1,0) -- Added .Value after get object to get the updated value
5end

Hope it helped, Errors? tell-me on comments

Wiki pages

Changed


Solved your problems? Accept a answer or put [SOLVED] in title

Answer this question