For my soccer game, I have a power meter for shooting/passing the ball and it works but it’s not accurate for some reason. I want it to have these zones (green for light passes, yellow for strong passes, and red for just sending it) I want to find the zone the slider lands in so I can put the right amount of power on the ball.
The meter itself is just an image so I put Guis inside the meter that cover each zone.
The anchor points of the Guis are at the top so in this script I calculate the height of the Guis compared to the slider
local height = slider.Position.Y.Scale
if height > green.Position.Y.Scale then --since udim decreases as u go up, the higher the value the lower the gui--
print ("GREEN!!")
elseif height > yellow.Position.Y.Scale then
print("yellow")
else
print("red")
end
This mostly works, but my problem is that when i try to use it, sometimes its inaccurate. It will print “green” when its in the yellow zone. It won’t print “red” at all. robloxapp-20240723-2050253.wmv (1.3 MB)
The slider isn’t a child of powerbar, so I don’t think the scale values aren’t relative to the same thing.
It seems like the slider’s scale is relative to the screen, while the labels are relative to the powerbar bar.
I would create an invisible frame and set the frame to the size you want for the powerbar, then parent the powerbar to the frame (with size scale = 1, 1), then parent the 3 labels to the frame, and finally parent the slider to the frame.
This way, all the objects have scale relative to the invisible frame, but you can also have the slider show on top of the bar.
As a general practice, you should consider UI as a “reactive” component of programming rather than an active component. You’re using the direct position of the frame to determine the power, whereas you should use a variable that’s shared between the two processes you’re interacting with (The slider moving and the deciding factor of the power).
local power = 0
local deltaT = 0
repeat
power = 0.5 - 0.5 * math.cos(2 * math.pi * deltaT)
frame.Position = UDim2.new(0.5, 0, power, 0)
deltaT = deltaT + task.wait(0.1)
until clicked
In this example, we’re figuring out the position of the UI based on the power variable instead of the other way around. This way, you can compare the power to the values you determine as the right zone (i.e. if power > GREEN_ZONE_LIMIT then...), and should be accurate across the entire flow.
Edit: Seems like I was focusing on a different problem, but glad the original issue was solved!