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 5 years ago
Edited 5 years ago
local Bar = script.Parent
local Value = script.Parent.Parent.Enrg.Value
while true do
    wait(0.01)
        Bar.Size = UDim2.new(Value/100,0,1,0)

    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 — 5y
0
Thank you!!! XD tightanfall 110 — 5y
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 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago
local Bar = script.Parent
local Value = script.Parent.Parent.Enrg -- Removed the .Value
while true do
    wait(0.01)
        Bar.Size = UDim2.new(Value.Value/100,0,1,0) -- Added .Value to the Udim2 so the value would 
-- be re counted every 0.01 second!

    end
Ad
Log in to vote
0
Answered by
yHasteeD 1819 Moderation Voter
5 years ago
Edited 5 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:


--<<<< Remember to change the value with server >>>>-- -- Updating local MyValue = workspace.MValue.Value -- If change the value will not update, you need to update the value. while true do MyValue = workspace.MValue.Value -- Update variable to set new value print(MyValue) wait() end -- Updated local MyValue = workspace.MValue -- here get the object of your value while true do -- Because the object is saved in the variable, you can get the updated value at any time. But you need to add MyValue.Value: print(MyValue.Value) -- Print the updated value wait() end

Correction

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

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

Also you can detect if value changed with Instance.Changed

  • This simple detect if Anything in object changed.

Example:

local obj = workspace.MyValue

obj.Changed:Connect(function() -- On change value start a function
    print("Changed the Value of " .. tostring(obj.Name) .. "!")
end)

For your script is here:

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

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