Need help on making a fuel gauge work

I would like to have this fuel gauge change place depending on a fuel value.

I’ve tried a script like this but it tweens it the wrong way.

local tweenservice = game:GetService("TweenService")
local tweeninfo = TweenInfo.new(15, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, -1, true, 0)

local tween1 = tweenservice:Create(script.Parent, tweeninfo, {Position = Vector3.new(-250.967, 8.899, 939.058)}):Play()
local tween2 = tweenservice:Create(script.Parent, tweeninfo, {Orientation = Vector3.new(18.838, -90, 0)}):Play()

Video:

External Media

I have a fuel value where the gauge needle should move when it changes.
Full value: 10500
Empty value: (should be) 0

The gauge in question:

If I missed anything out please tell me!
EDIT: This gauge is for a generator system and stays in one place

5 Likes

Your tweens not working because the position has to be relative to the cars current rotation.
if the car is rotated 26.432 degrees on an axis on account of the fact its a vehicle your tween has to account for that.

i forget exactly how to do it but look up tutorials on cframe.Lookvector that should get you going in the right direction🫡

1 Like

Thanks for the info!

It should be easier as the gauge doesn’t move tho right? (For generators, stays in one place)
EDIT: I see why you thought it’s for a car. I forgot to add what it is for in the post.

1 Like

oh it doesnt move? bet. give me 5

Generator.rbxm (4.6 KB)

This should work

Thanks, ill try it shortly!

Used the script (no changes) and it put it the wrong way
image

What should I change to change the orientation

1 Like

Try using welds that may be alot easier

How would I accomplish that?

Can you send me your model and ill take a look at it then guide you through?

If it was moving, I would rig it with a Motor6D and then simply change the Transform property on relative clients.

If it’s not moving, I would anchor it and rotate it manually.

Here’s the CFrame code I would start with:

local guage = script.Parent
local pointer = guage.Pointer
local empty = pointer.CFrame
local full = pointer.CFrame * CFrame.Angles(0,1.4,0)

-- pointer.CFrame = empty:Lerp(full, percentTank)
1 Like

you can use Rigid Constraint to make that
click on Model tab
select the dropdown from here
{A5A736A3-1629-401F-913C-49985B598F85}
pick Rigid Constraint
{289E5ED3-507A-42F1-BA33-ACD786C50902}
now click on the edge of the red neon pointer and the 2nd time on the black cylinder
anchor the black cylinder and unanchor the red pointer

now rotate the black part and the red pointer will rotate with it

1 Like

This is the result:
image

Also changing the fuel value doesn’t make it move for some reason.

local guage = script.Parent
local pointer = guage--.Pointer
local empty = pointer.CFrame
local full = pointer.CFrame * CFrame.Angles(0,1.4,0)
CurrentPercentTank = game.ReplicatedStorage.Generators.Fuel.Values.MainGensFuel.Value

pointer.CFrame = empty:Lerp(full, CurrentPercentTank)
1 Like

Are you sure youre updating it every time the fuel val is changed?

1 Like

ah, no i was not!

That fixed it not moving at all but it’s still going around the wrong direction.
image

1 Like

Not sure if this is right… not tested.
I use rotation for this.

local generator = script.Parent
local fuelLevel = generator:WaitForChild('FuelLevel')
local fuelGaugePart = generator:WaitForChild('FuelGauge')
local maxFuel = 10500
local totalAngle = 150

fuelLevel:GetPropertyChangedSignal('Value'):Connect(function()
	local rotationAngle = totalAngle * (fuelLevel.Value / maxFuel) - totalAngle / 2
	fuelGaugePart.Rotation = Vector3.new(0, 0, rotationAngle)
end)

You can change the pivot point for the needle in the studio from MODEL/Edit pivot.

Try this.

local pivotCF = pivotPart.CFrame -- (this is the central knob around which the dial rotates)
local emptyAngle = 45
local totalRotationAngle = 90 -- (the amount it rotates + the empty angle, so this case will have a max rotation of 135 deg)
local offset = gauge.Size.Y / 2 -- (might have to change this to X or Z depending on how your mesh is made)

fuel.Changed:Connect(function(fuelQuantity)
    local ratio = fuelQuantity / maxFuel
    local newAngle = emptyAngle + (ratio * totalRotationAngle)
    local newCF = pivotCF * CFrame.Angles(0, 0, newAngle) -- might have to flip the axes here asw)
    newCF *= CFrame.new(0, offset, 0)
    dial.CFrame = newCF
end)

Could you explain the offset a little more? I don’t quite understand what that is.

Offset is basically the radius of the imaginary circle in which your dial/hand rotates. Imagine a circle around the central black knob, and the radius of this circle extends upto the center of the gauge. Which means the radius is half the Y size of the gauge.


The blue line is the offset here, radius of the circle, red line is your actual dial and the white dot is the centre. Notice how the circle passes through the centre of the dial and not on through its tips.