So, a few things here:
On line 4 you set a variable for storing the Y-offset of your GUI. This variable is now a constant, and will not update as the state of your GUI changes.
- If you wanted to go this route, you would need to re-assign that variable to the Y-offset continuously with a loop or event of some sort.
I notice you have a Meter variable declared at the top of your code, but it's not being used. I'm assuming this is a ValueObject that represents your temperature. If so, you should be using that!
- (As an aside: 9 times out of 10 you shouldn't need to use ValueObjects for changing state. You can just create the variable in your code and use BindableEvents/Functions to update them across scripts!)
With these considerations in mind, let's break it down from the beginning:
Step 1): Create some state for your temperature bar
Let's start off by creating and grouping some data that is related to the temperature bar:
local meterState = {
temperature = 0,
maxTemperature = 120,
minTemperature = -32,
stages = {
stage1 = "rbxassetid://11946049186",
stage2 = "rbxassetid://11946059093",
stage3 = "rbxassetid://11946060289"
}
}
This way you can access all temperature-related data by doing things like meterState.temperature or meterState.stages. I also added a maxTemperature and minTemperature value, because your meter probably doesn't go from infinity to negative infinity.
Step 2): Create some way to update your temperature state
Obviously, you could just update your temperature value by doing something like meterState.temperature += 1, but that still leaves us with quite a few problems, such as updating the GUI.
A good solution to this would be to create a function that updates the temperature value and GUI at the same time!
local bar = script.Parent
local updateTemperature = function(newTemp)
meterState.temperature = newTemp;
local min = meterState.minTemperature;
local max = meterState.maxTemperature;
bar.Size = UDim2.new(1, 0, 1/(max - min)*(newTemp - min), 0)
end
I'm also throwing a bit of math in there to compute the size of the temperature bar. The 1/(max - min)*(newTemp - min) is a common formula for computing the percentage of a number range when given a certain value in that number range.
I have a YouTube video on this that you can watch here.
Now you can update your temperature value and your GUI at the same time with one function call! For example, this will fill the bar up all the way vertically:
and this will completely shrink the bar vertically:
Step 3): Apply different images based on temperature
I didn't see you setting any images in your code so I'm not sure where you would want to apply them, so I'm just going to psuedo-code that part: But first, let's add a bit more information to our meterState so we can update our image accordingly with our temperature:
local image = script.Parent.Parent
local meterState = {
temperature = 0,
maxTemperature = 120,
minTemperature = -32,
stages = {
{ 0, "rbxassetid://11946049186" },
{ 60, "rbxassetid://11946059093" },
{ 100, "rbxassetid://11946060289" }
}
}
Now, let's add the logic for changing our image once we update the temperature:
local bar = script.Parent
local image = script.Parent.Parent
local meterState = {
temperature = 0,
maxTemperature = 120,
minTemperature = -32,
stages = {
{ 0, "rbxassetid://11946049186" },
{ 60, "rbxassetid://11946059093" },
{ 100, "rbxassetid://11946060289" }
}
}
local updateTemperature = function(newTemp)
meterState.temperature = newTemp;
local min = meterState.minTemperature;
local max = meterState.maxTemperature;
bar.Size = UDim2.new(1, 0, 1/(max - min)*(newTemp - min), 0)
local stages = meterState.stages;
for index = 1, #stages do
local stage = stages[index];
local stageMin, stageImg = unpack(stage);
if (newTemp <= stageMin) then
image.Image = stageImg
break;
end
end
end
And that's it! Now you can update your temperature through the updateTemperature function and it will handle all of your UI state changes.
If you need to update your temperature through a different script, I recommend looking into bindable events/functions like I mentioned above.
Hope this helps, good luck!
1 comment
When you set
barsizeon line 4, that value is now a constant. It represents a snapshot of what bar.Size.Y.Offset was, but not what it is. You must continually update that variable if you want to record change. However, I would avoid using GUI state as a metric for detection as there are better alternatives.