I need help with a gas gauge for a car in Roblox.
Setup:
DriveSeat (VehicleSeat) and GasValue (NumberValue) are in the same car model.
I have a GasGUI with a TextLabel that displays the gas percentage.
I used a LocalScript
in the TextLabel
.
The issue is that the script only shows “Gas: –%” even when I enter the car, and I want it to show how much gas is in the car.
local player = game.Players.LocalPlayer
local gasLabel = script.Parent
gasLabel.Text = "Gas: --%"
local function onDriveSeatChanged(driveSeat)
if driveSeat.Occupant and driveSeat.Occupant.Parent == player.Character then
local car = driveSeat.Parent
local gasValue = car:FindFirstChild("GasValue")
if gasValue then
gasLabel.Text = "Gas: " .. math.floor(gasValue.Value) .. "%"
gasValue:GetPropertyChangedSignal("Value"):Connect(function()
gasLabel.Text = "Gas: " .. math.floor(gasValue.Value) .. "%"
end)
else
gasLabel.Text = "Gas: N/A"
end
else
gasLabel.Text = "Gas: --%"
end
end
I’m not sure what isn’t working.
2
In the function you don’t check if the driveSeat
parameter is a seat object. Nestle the rest of your code with if if driveseat:IsA("Seat") then
. Also you don’t connect the function in your code. Connect the function via this seat.Sat:Connect(OnDriveSeatChanged)
. Make sure your code is called correctly and that unknown parameters are used as objects.