Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How To Get A Car's Speed While It's Driving?

Asked by 9 years ago

I'm currently making a Script that when the front of my car lip hits something at a certain speed or greater, it will break the whole car. Except I've tryed using MaxSpeed, but because it's set to 201 and I drive at 20 SPS (Studs Per Second) with throttle 1, the car breaks when it hits something at a slow speed. Below is my current Script:

function Touch(onTouched) if game.Workspace.Car.VehicleSeat.Throttle == 1 and game.Workspace.Car.VehicleSeat.Speed > 100 then game.Workspace.Car:BreakJoints() end end script.Parent.Touched:connect(Touch)

2 answers

Log in to vote
2
Answered by
Tkdriverx 514 Moderation Voter
9 years ago

To get the speed, just simply do seat.Velocity.magnitude (assuming seat is the VehicleSeat).

"magnitude" is a property of a Vector3 which gets the magnitude (length) of the vector. And since Velocity is a Vector3, whatever the velocity is, it will have a magnitude equal to it's "length," which in this case would be the speed in studs per second.

I have actually discovered that this is the way that the VehicleSeats display the speed when you have the HeadsUpDisplay enabled.

Ad
Log in to vote
3
Answered by 9 years ago

As far as I am concerned, VehicleSeat.Speed (property) is not existant. You could track the speed manually in studs per second by use of mangitude, and set aclimit that way.

I'd advise you to not use game.Workspace.Car when assigning your target - it will require you to rename all the defined areas if you change name of the car, and can even cause issues / conflicts if you have several vehicles named Car in your game (or if the player 'Car' joins). If the script is already parented to the car (in the front), you should navigate through the hierarchy from there.

Since I have no idea how your hierarchy works for the car, I'll just assign a variable to it for now. Feel free to change it to your liking.

local vehicle = game.Workspace.Car
local speed = 0 -- default at 0
local speedForCrash = 20 -- decides the 'breaking limit'.
local refreshRate = 0.5 -- Every 0.5 second
local lastPos = vehicle.VehicleSeat.Position

script.Parent.Touched:connect(function(hit)
    if (hit and (vehicle.VehicleSpeed.Throttle == 1) and (speed > speedForCrash)) then
        vehicle:BreakJoints()
    end
end)

-- Putting the loop after we've declared the connection for Touch, so we don't have to run a new thread for the loop. 

-- We're taking use of magnitude  - the distance between two Vector3s. 
-- We'll divide it by the refreshRate to get Studs Per Second
while true  do -- can potentially also do "while wait(refreshRate) do", but I like having the wait at the end.
    local magn = (vehicle.VehicleSeat.Position - lastPos).magnitude
    speed = magn/refreshRate -- updating our speed-variable.
    print(speed)
    lastPos = vehicle.VehicleSeat.Position
    wait(refreshRate)
end


0
You can just do seat.Velocity.magnitude. (assuming 'seat' is the VehicleSeat) It's a Vector3, too. It's a quicker method and easier to read. Tkdriverx 514 — 9y
0
Raven, this is the old way of doing it, until it was discovered Velocity.magnitude can be done to get speeds. Tkdriverx 514 — 9y
0
Oh! Then I learned something new today, too! Awesome. Thanks for letting me know. Ravenshield 180 — 9y
0
I know, you are a user from, well, I remember your name all over the place way back when. So I figured you still did things old-fashoned. So, it's good to learn the modern methods. :P Tkdriverx 514 — 9y

Answer this question