I’m attempting to make a gas system, Where if the car is going above the speed of 1 MPH it subtracts the gas value from 100 to 0 and the car no longer works if the gas level is at 0. My plan is just have everything run off of a int value, so a gui gets that number, and when you refill it, it just refills that number.
The main issue is I don’t know how I would approach this.
I have looked for some solutions, but none really worked for me personally. I don’t wanna use a free model or anything either.
So here is the local script, and after that is the server script.
Local script:
local seat = car.DriveSeat
if seat.Throttle >1 then
script.GasEvent:FireServer()
end
Server script:
local event = script.Parent.GasEvent
local car = script.Parent.Car.Value
local seat = car.DriveSeat
value.Value = 100
event.OnServerEvent:Connect(function()
if seat.Throttle >1 then
value.Value = 100-1
end
end)
It appears the gas isn’t draining because the value is reset to 99 every time the event is fired. 100 - 1 = 99. To fix that, replace that line with value.Value = value.Value - 1.
Seat.Throttle refers to if the throttle is on.
If you want to check if the car is going above 1 mph (Roblox speeds are actually Studs per Second, not MPH) you’d wanna do:
if seat.Velocity.Magnitude > 1 then
script.GasEvent:FireServer()
end
There’s some other issues with your code, although it’d probably be best to show you via an example of such a system already out there for free. The Racing Community uses a pretty rudimentary system, similar to what you’re looking to accomplish.
I’ve also very quickly concocted up my own for your use. It’s very basic with what you wanted. Notably, no Local Scripts needed to be used, nor any Events. As far as creating a GUI that will get the number, you could try naming the car to the user’s name. I usually do “PlayerNameCar.” Then have the local script inside the GUI look for that car, then go into the seat, and find the fuel value, to go from there.
@GeorgeOfAIITrades already thought of my suggestion! Haha. As well as using Velocity you should also check server sided. You can use the RunService.Heartbeat event which fires after physics calculations so that your value reduces smoothly based on the distance the car travels. You can connect to the event and do your velocity check in a server script. This will prevent exploiters from firing the remote or stopping it from firing at all.
Since Velocity.Magnitude is speed in studs per second you need to reduce its value. Heartbeat passes a value to its callback. This value represents the number of seconds since the last Heartbeat. To reduce the speed you can multiply the speed by the delta to estimate the distance traveled in that physics frame: Velocity.Magnitude / maxCarSpeed * deltaTime * gasPerSecond
This will firstly get the percentage to full speed in decimal form. Then it gets multiplied by deltaTime. Since deltaTime is about 1/60 it fires about 60 times in a second. So if the car travels at its max speed the percentage in decimal form is 1. So the value is decreased by 1 * 1/60 * gasPerSecond. If it happens 60 times it’s multiplied by 60: 1 * 60/60 * gasPerSecond. So now it’s 1 * gasPerSecond (just gasPerSecond) and it has been one second!
Now you can subtract this speed from your gas value and it will drain at 1 gas per second. Make sure you use a NumberValue not an IntValue if you do this though since a number less than 1 will be subtracted meaning your value will round back down and never increase or decrease. For example, adding 0.2 to 0 will get 0.2 but 0.2 isn’t an integer so it’s rounded down to 0 again.
Sorry if this sounds a bit complicated the way I explained it. I’d be happy to help with anything you don’t understand!