How to make a horizontal meter/gauge?

Hi, sorry if the title is confusing, I’m not exactly sure what they are called.

I’m trying to make an RBMK game (as seen in my last post) and now I’m moving onto more specific stuff, controls, gauges, etc. I need to make a gauge similar to this one.

Every time I’ve tried to write the code, it either flies completely out of the meter in the wrong direction, or just goes one way forever.
image

“Gauge” is the part that moves.
image

Anyone know how to do this?

This is my current code:

--// Variables
local ReactorModule = require(game.ServerScriptService.Reactor)
local Meter = script.Parent
local Gauge = Meter.Gauge

local MeterSettings = {
	["MeterLength"] = 1,
}

--// Main
local LastValue

wait(5)

while true do
	ReactorModule.AverageRodPositon = math.random(1, 100)
	local NewValue = ReactorModule.AverageRodPositon
	
	if NewValue == LastValue then
		return
	end
	
	local function CalculateNewPositon()
		local XPosition = Gauge.Position.X
		local YPosition = Gauge.Position.Y
		local ZPosition = (Gauge.Position.Z + (MeterSettings.MeterLength/ReactorModule.AverageRodPositon/100))
		
		return Vector3.new(XPosition, YPosition, ZPosition)
	end

	local Tween = game.TweenService:Create(Gauge, TweenInfo.new(0.3), {Position = Gauge.CFrame.LookVector + CalculateNewPositon()})
	Tween:Play()
	Tween.Completed:Wait()
	
	LastValue = ReactorModule.AverageRodPositon
	
	wait()
end

Thanks for any and all help!

1 Like

You are repeatedly adding a value to the Gauge’s Position.Z, causing it to increase indefinitely.
You can fix this by specifying a reference position which does not change and adding the value to that.

The way you’re calculating the new position also looks wrong, you probably want to multiply the AverageRodPositon with the MeterLength.

More code comments that aren't directly about getting it working
  • ReactorModule.AverageRodPositon has a typo.
  • If you get the same value twice in a row, the code returns, breaking out of the loop, is that intentional?
  • CalculateNewPositon doesn’t need to be it’s own function, just set the variables in the loop itself.
  • Consider using the range from 0 to 1 instead of percentage values.

Here’s some changes to make it work, insert where appropriate.

--// Main
local referencePosition = Gauge.Position
	local function CalculateNewPositon()
		local XPosition = Gauge.Position.X
		local YPosition = Gauge.Position.Y
		local ZPosition = (referencePosition.Z + (MeterSettings.MeterLength * (ReactorModule.AverageRodPositon)/100))

		return Vector3.new(XPosition, YPosition, ZPosition)
	end

Yep, this worked! Thank you for saving me a headache!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.