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)
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.
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