How could I make this odometer script work?

Hi! I’m trying to make an odometer for an a-chassis plugin and the UI doesn’t seem to pop up when I need it, I really do not know what to do.

Screenshot 2023-03-24 082007

code below:

local MaxMileage = 1500 -- This is your max mileage, I suggest leaving it at 2.5k
local MM = script.Parent.Parent.Parent.Parent:WaitForChild("DriveSeat").Clocked.Value

local car = script.Parent.Parent.Car.Value
local _Tune = require(car["A-Chassis Tune"])
local DriverSeat = car:WaitForChild("DriveSeat")
local event = car:WaitForChild("Server")
local IsOn = script.Parent.Parent.IsOn
local UI = script.Parent.Clocked_M.Text

game:GetService("RunService").Stepped:Connect(function()
	if not IsOn.Value then DriverSeat = DriverSeat; event:FireServer(DriverSeat) return end 
	UI = tostring(typeof(0 == MM)).. "<font size='16'>		Miles	</font>"
	if MM == MM then 
		MM = 0 
		MaxMileage = MaxMileage
		MM = MM + DriverSeat.Velocity.Magnitude
	 if DriverSeat.Velocity.Magnitude > 500 then 
			UI = tostring(typeof(0 == MM)).. "<font size='16'>		Miles	</font>"
			MM = 1
			if MaxMileage == MM + 1500 then 
				DriverSeat.Enabled = false 
			end
		end
	end
end)

***but wait there's more! heres the source code for the "handler" script inside the event;

```local car = script.Parent.Parent.Parent.Parent:WaitForChild("DriveSeat")
local DriveSeat = car
script.Parent.OnServerEvent:Connect(function(plr, val)
	DriveSeat.Clocked.Value = val
end)```

Thank you for your time.
1 Like

I think I am seeing the problem here with this line:

and this line:

The problem is that the variable UI isn’t really referring to the property. Instead, UI is referring to Clocked_M’s text when the script starts running. To solve this issue, you could replace the first line I mentioned with this:

local UI = script.Parent.Clocked_M -- Basically remove the .Text

Then when you want to change Clocked_M’s text, you do something like this:

UI.Text = tostring(typeof(0 == MM)).. "<font size='16'>		Miles	</font>"

So after these changes, your code should look something like this:

local MaxMileage = 1500 -- This is your max mileage, I suggest leaving it at 2.5k
local MM = script.Parent.Parent.Parent.Parent:WaitForChild("DriveSeat").Clocked

local car = script.Parent.Parent.Car.Value
local _Tune = require(car["A-Chassis Tune"])
local DriverSeat = car:WaitForChild("DriveSeat")
local event = car:WaitForChild("Server")
local IsOn = script.Parent.Parent.IsOn
local UI = script.Parent.Clocked_M

game:GetService("RunService").Stepped:Connect(function()
	if not IsOn.Value then DriverSeat = DriverSeat; event:FireServer(DriverSeat) return end 
	UI.Text = tostring(typeof(0 == MM.Value)).. "<font size='16'>		Miles	</font>"
	if MM.Value == MM.Value then 
		MM.Value = 0
		MM.Value = MM.Value + DriverSeat.Velocity.Magnitude
		if DriverSeat.Velocity.Magnitude > 500 then 
			UI.Text = tostring(typeof(0 == MM.Value)).. "<font size='16'>		Miles	</font>"
			MM.Value = 1
			if MaxMileage == MM.Value + 1500 then 
				DriverSeat.Enabled = false 
			end
		end
	end
end)

While I was editing your code, I realized that the variable MM also had the same problem too. So I guess your takeaway from this is to not create variables pointing to properties if you want to update the properties. Rather, make the variable point to the object so you can easily access the most recent value of the property (Object.Property) and change the value of the property (Object.Property = XXX). Hope this helps.

Hey I’m sorry it took a while to get back to you, it almost worked though I got confused when you said "Rather make the variable point to the object so you can easily access the most recent value of the property (Object.Property) and change the value of the property (Object.Property = XXX).

This did help however I got “boolean miles”

Basically what I meant was that when you set the property (like Text) of an object (like this TextLabel) to a variable, changing the variable won’t change the property value. So if you want to change the TextLabel’s property, you first set the TextLabel to a variable, then you can do something like TextLabel.Text = “Whatever You Want”.

Back to your problem though. That’s because of this line of code that you wrote:

Even though I changed it for you to make the code run, but you want the number of miles to show right? So when you wrote typeof(0 == MM.Value), it will be evaluated as a boolean. To be honest, I don’t know why you need to use typeof. You can replace that line of code with:

UI.Text = tostring(MM.Value) .. "<font size='16'>		Miles	</font>"

So your final code should look something like this:

local MaxMileage = 1500 -- This is your max mileage, I suggest leaving it at 2.5k
local MM = script.Parent.Parent.Parent.Parent:WaitForChild("DriveSeat").Clocked

local car = script.Parent.Parent.Car.Value
local _Tune = require(car["A-Chassis Tune"])
local DriverSeat = car:WaitForChild("DriveSeat")
local event = car:WaitForChild("Server")
local IsOn = script.Parent.Parent.IsOn
local UI = script.Parent.Clocked_M

game:GetService("RunService").Stepped:Connect(function()
	if not IsOn.Value then DriverSeat = DriverSeat; event:FireServer(DriverSeat) return end 
	UI.Text = tostring(typeof(0 == MM.Value)).. "<font size='16'>		Miles	</font>"
	if MM.Value == MM.Value then 
		MM.Value = 0
		MM.Value = MM.Value + DriverSeat.Velocity.Magnitude
		if DriverSeat.Velocity.Magnitude > 500 then 
			UI.Text = tostring(MM.Value).. "<font size='16'>		Miles	</font>"
			MM.Value = 1
			if MaxMileage == MM.Value + 1500 then 
				DriverSeat.Enabled = false 
			end
		end
	end
end)

I mean I used typeof and thought it was a solution since whenever I turn the car on the gauge does not pop up even after applying your edit of my code, so all in all I got really confused but I’ll definitely mark your post as a solution.

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