I want to make a GUI that says how many MPH you’re going in your Vehicle, and I only want the GUI to pop up when I’m sitting in the seat. I don’t know how I would measure that, but I want the script to be in the Vehicle Seat. How could I accomplish this?
https://create.roblox.com/docs/reference/engine/classes/Seat
Detect a change in seat.Occupant
local seat = script.Parent
seat.GetPropertyChangedSignal("Occupant"):Connect(function()
local character = seat.Occupant
local player = game.Players:GetPlayerFromCharacter(character)
-- gui code here
end)
if i solved your problem, please consider pressing the solution button under my reply
Slight issue in your code. seat.Occupant has the humanoid of the player, not the character. Character should instead be defined as
local character = seat.Occupant.Parent
Also, the start of the function should check if the occupant actually exists, and the event wasn’t fired by someone leaving the seat.
Something using
if seat.Occupant then
-- normal code
else
-- player left the seat code
end
would help ensure it is correct
No, I already know how to detect if the player is sitting in a seat. I want to know how to measure how fast the vehicle is going and put the Value on a GUI.
Using the power of physics, the formula for speed is the distance over time. You can use Heartbeat to track how fast the vehicle is moving by taking
(position1 - position2)/(amount of time you want to wait)
I want a script, not a math problem.
Ok, first of all, you are not entitled to a script. As a matter of fact, we aren’t supposed to give you direct scripts:
Anyway, you’re in luck because not much math is needed… If you want it in studs/s, that is.
You can find the velocity by checking the player’s character model’s AssemblyVelocity
. To get it as a scalar, just use AssemblyVelocity.Magnitude
20 studs is 1 meter. Therefore, mps = sps / 20
To convert m/s into mi/hr, you just multiply by 2.237
. (From Google)
If you just want the answer, here it is:
-- THIS IS A SCRIPT WITH RunContext SET TO Client
local seat = script.Parent
-- seat.Occupant will be nil at first so you need to check it via GetPropertyChangedSignal,this is just an example
local char = seat.Occupant.Parent
local occupantPlayer = game.Players:GetPlayerFromCharacter(char)
local carGui = occupantPlayer.PlayerGui.YourCarGui
local mphCounter = carGui.MphCounter
local carRoot = ... -- This is the root part of your car or vehicle.
local velStps = carRoot.AssemblyVelocity
local velMps = velStps / 20
local velMiphr = velMps * 2.23
mphCounter.Text = `Mph: {velMiphr}`
print("Tada", velMiphr)
You’ll need to do this in RunService.Heartbeat
.
Sorry if I went off the rails at the beginning there.
EDIT: I made changes to this script as my previous example was wrong. Don’t know what was wrong with me
Sorry, I’m a beginner scripter, and I don’t know how to do all that, which is why I was asking for a script because I don’t know how to do it at all, and there’s no tutorials on YouTube or anyway where else.
Ok, but ask nicely next time.
I also apologize for my outburst, I have edited my original comment with an example.
Of course, seat.Occupant is going to be nil when the script first runs, you have to check whenever Occupant changes (This happens whenever a player gets in or out of the seat) via seat:GetPropertyChangedSignal("Occupant"):Connect ...
and then set occupant
(your variable) to seat.Occupant
inside it.
Then, in a seperate block, create a RunService.Heartbeat
connection and check if occupant is not nil, in that case you can use the code I provided.
Try to implement that and share the code you got so I can check it for errors. Or, you can run it in Studio and try to check what’s wrong as well.
Once again, I’m sorry if I stressed you out.
*EDIT: If your script isn’t working, I edited my previous comment because I did some things incorrectly, sorry.
Thank you so much for the script! But where do I put the GUI?
No problem!
You could create a gui and put it under the script as some name, e.g. “CarGui”.
BTW, make sure you script runs with “Client” RunContext and make sure that in the seat.Occupant change make it check that the new occupant is LocalPlayer.
You can then copy the CarGui into the player.PlayerGui when the script starts and set its Enabled property to false.
When the occupant changes and it is == to LocalPlayer, set the Enabled property to true. Otherwise set it to false.
Something like this:
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local player = Players.LocalPlayer
local gui = script.CarGui:Clone()
gui.ResetOnRespawn = false
gui.Parent = player.PlayerGui
local seat = script.Parent -- Or wherever it is
function updateDisplay()
local velMiphr = ... -- The code from before here.
gui.MphCounter.Text = `Mph: {velMiphr}`
end
local updateConnection
seat:GetPropertyChangedSignal("Occupant"):Connect(function()
if seat.Occupant == player.Character.Humanoid then
player.PlayerGui.CarGui.Enabled = true
updateConnection = RunService.Heartbeat:Connect(updateDisplay)
else
player.PlayerGui.CarGui.Enabled = false
if updateConnection then
updateConnection:Disconnect()
end
end
end)
In this example, I hook the Heartbeat event whenever the player sits in the car, and disconnect it when they jump out. This is different from my previous examples. Sorry for my inconsistency.
Oh yes well that is the code that I sent before, you know the one to calculate mph?
But, you have to set local carRoot = to the root part of the car. This is very important or it won’t owrk.
I think they just got the wrong property name, I’m pretty sure it should be gui.ResetOnSpawn
instead of gui.ResetOnRespawn
If it is a LocalScript
then you need to wait for its children to load in using WaitForChild()
That error message is saying that the player
variable is nil, so player = Players.LocalPlayer
is not working as intended.
The LocalPlayer variable is only functional from code running on the client, so what you should do is remake this script as a LocalScript
and place it under StarterPlayerScripts or StarterGui (up to you).
You’ll also have to change the local seat = ...
to where the seat is.
So basically how would the entire script look like? Is it okay if you could re-write it?