Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

I need help with this script please.

I need help with a gas gauge for a car in Roblox.
Setup: DriveSeat (VehicleSeat) and GasValue (NumberValue) are in the same car model. A GasGUI with a TextLabel displays the gas percentage. Problem:
The script only shows "Gas: --%" even when I enter the car.
Script Snippet:


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``` What am I doing wrong? I used a LocalScript in the TextLabel.
I want it to show how much gas is in the car, but it just says ""Gas: --%""

Please Help me.
 
Hey there.
Can you please edit your original post with your code formatted and in code blocks so we can better read your code.

To get code blocks you can use the </> button in the toolbar, markdown, or BBCode.
 
I may be wrong as i have only dabbled in roblox a few times but looking at your script. But looking it looks like your not checking for seat changes correctly and i would say you need to check the gas levels when they change seats, give this a try.

Code:
local gasLabel = script.Parent -- The TextLabel displaying the gas level
gasLabel.Text = "Gas: --%" -- Default text before the player enters the car

-- Function to update the gas value
local function updateGasLabel(gasValue)
    gasLabel.Text = "Gas: " .. math.floor(gasValue.Value) .. "%"
end

-- Function to detect when the VehicleSeat changes
local function onDriveSeatChanged()
    local driveSeat = script.Parent.Parent:FindFirstChild("DriveSeat")
    if driveSeat then
        driveSeat:GetPropertyChangedSignal("Occupant"):Connect(function()
            -- Check if someone is in the seat
            if driveSeat.Occupant then
                local car = driveSeat.Parent
                local gasValue = car:FindFirstChild("GasValue")

                if gasValue then
                    -- Update the gas label and track changes
                    updateGasLabel(gasValue)
                    gasValue:GetPropertyChangedSignal("Value"):Connect(function()
                        updateGasLabel(gasValue)
                    end)
                else
                    gasLabel.Text = "Gas: N/A"
                end
            else
                gasLabel.Text = "Gas: --%" -- When no one is in the seat
            end
        end)
    else
        gasLabel.Text = "Gas: --%" -- If the seat isn't found
    end
end

onDriveSeatChanged()

I added detection for when the player enters/exits the seat and made sure it listens for changes in the gas value. Should now update the gas percentage correctly. Let me know if that works!
 
I may be wrong as i have only dabbled in roblox a few times but looking at your script. But looking it looks like your not checking for seat changes correctly and i would say you need to check the gas levels when they change seats, give this a try.

Code:
local gasLabel = script.Parent -- The TextLabel displaying the gas level
gasLabel.Text = "Gas: --%" -- Default text before the player enters the car

-- Function to update the gas value
local function updateGasLabel(gasValue)
    gasLabel.Text = "Gas: " .. math.floor(gasValue.Value) .. "%"
end

-- Function to detect when the VehicleSeat changes
local function onDriveSeatChanged()
    local driveSeat = script.Parent.Parent:FindFirstChild("DriveSeat")
    if driveSeat then
        driveSeat:GetPropertyChangedSignal("Occupant"):Connect(function()
            -- Check if someone is in the seat
            if driveSeat.Occupant then
                local car = driveSeat.Parent
                local gasValue = car:FindFirstChild("GasValue")

                if gasValue then
                    -- Update the gas label and track changes
                    updateGasLabel(gasValue)
                    gasValue:GetPropertyChangedSignal("Value"):Connect(function()
                        updateGasLabel(gasValue)
                    end)
                else
                    gasLabel.Text = "Gas: N/A"
                end
            else
                gasLabel.Text = "Gas: --%" -- When no one is in the seat
            end
        end)
    else
        gasLabel.Text = "Gas: --%" -- If the seat isn't found
    end
end

onDriveSeatChanged()

I added detection for when the player enters/exits the seat and made sure it listens for changes in the gas value. Should now update the gas percentage correctly. Let me know if that works!
I tried that but it still won't work, I have the script as a LocalScript inside a TextLabel That is inside a Frame.
Is that why it's not working?
 
i think you find its due to the localscript, i know they can only run in certain places. can you make sure the GUI is in StarterGui folder
The GUI is not in the starter GUI folder because I need it inside the car Plugins folder so will display the gas amount only when your in the car.
Will this script still work if I use it as a Regular Script?

Thanks,
thegamer_guy
 
Hey,

That's probably the issue. LocalScripts only run in certain places like StarterGui, PlayerScripts, or inside the player's character. Since your GUI and LocalScript are inside the car's Plugins folder, the LocalScript isn't running at all.

Using a Regular Script won't help because Regular Scripts can't directly manipulate a player's GUI. What you can do is move the GUI to StarterGui so it gets added to each player's PlayerGui when they join the game. Then, you can control the visibility of the GUI based on whether the player is in the car.

To make the GUI show only when the player is in the car, you can use a RemoteEvent to communicate between the server and the client.
 
Hey,

That's probably the issue. LocalScripts only run in certain places like StarterGui, PlayerScripts, or inside the player's character. Since your GUI and LocalScript are inside the car's Plugins folder, the LocalScript isn't running at all.

Using a Regular Script won't help because Regular Scripts can't directly manipulate a player's GUI. What you can do is move the GUI to StarterGui so it gets added to each player's PlayerGui when they join the game. Then, you can control the visibility of the GUI based on whether the player is in the car.

To make the GUI show only when the player is in the car, you can use a RemoteEvent to communicate between the server and the client.
ok I will try it, thank you so much for the help
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom