Hello, My question is basically what the title says. I have a local script inside of startercharscripts and I want it so that if a plr enters a seat and the seat’s parent is 1 of the available cars in Workspace.Vehicles it changes the settings depending on that car. (Ex: someone enters a tank (slow) then wants to go in a supercar (fast) and drive, but because of this script it makes the supercar as slow as the tank.)
Local Script:
local char = script.Parent
local humanoid = char:WaitForChild('Humanoid')
local WheelPositions = {
FL = Vector3.new(-2.5,0,-4.3),
FR = Vector3.new(2.5,0,-4.3),
RL = Vector3.new(-2.5,0,4),
RR = Vector3.new(2.5,0,4),
}
local SpringLengthMemory = {}
for i,v in pairs(WheelPositions)do SpringLengthMemory[i] = 0.5 end
local SuspensionMaxLength
local wheelRadius
local Stiffness
local Damper
local wheelFriction
local torque
local MaxSpeed
local SteerAngle
local SmoothSteer
humanoid.Seated:Connect(function(seated,seatPart)
if seated and seatPart then
if seatPart:FindFirstAncestor('Vehicles')and seatPart:IsA("VehicleSeat")then--whatever that gives out its car
local carModel = seatPart.Parent
local carPrim = carModel.PrimaryPart
if seatPart.Parent.Name == "CarModel2" then
SuspensionMaxLength = 1.3
wheelRadius = 1.25
Stiffness = 118
Damper = 6
wheelFriction = 5.4
torque = 12.4
MaxSpeed = 80
SteerAngle = 20.74
SmoothSteer = 0
elseif seatPart.Parent.Name == "CarModel" then
SuspensionMaxLength = 1.3
wheelRadius = 1.25
Stiffness = 120.5
Damper = 6.4
wheelFriction = 5.5
torque = 6
MaxSpeed = 55
SteerAngle = 20
SmoothSteer = 0
end
local serverTakeOverWait = 0
while true do
local delta = game["Run Service"].Heartbeat:Wait()
local carPrim = carModel.PrimaryPart
SmoothSteer = math.abs(
seatPart.SteerFloat-SmoothSteer)<=delta*5 and seatPart.SteerFloat or SmoothSteer+math.sign(
seatPart.SteerFloat-SmoothSteer)*delta*5
for wheelName, originalPosition in pairs(WheelPositions)do
local carCFrame = carPrim.CFrame
local rayOrigin = carCFrame:ToWorldSpace(CFrame.new(originalPosition)).p
local rayDirection = -carCFrame.UpVector * (SuspensionMaxLength + wheelRadius)
local rayParams = RaycastParams.new()
rayParams.FilterDescendantsInstances = {carModel}
local raycast = workspace:Raycast(rayOrigin,rayDirection,rayParams)
if raycast then
local RaycastDistance = (rayOrigin - raycast.Position).magnitude
local SpringLength = math.clamp(RaycastDistance - wheelRadius, 0, SuspensionMaxLength)
local StiffnessForce = Stiffness * (SuspensionMaxLength - SpringLength)
local DamperForce = Damper * (( SpringLengthMemory[wheelName] - SpringLength) / delta)
local SuspensionForceVec3 = carCFrame.UpVector * (StiffnessForce + DamperForce)
local RotationsOnlyWheelDirCFrame = CFrame.lookAt(Vector3.zero,carCFrame.LookVector,carCFrame.UpVector)
if string.sub(wheelName,1,1)=='F' then
RotationsOnlyWheelDirCFrame = RotationsOnlyWheelDirCFrame * CFrame.Angles(0,-math.rad(SmoothSteer * SteerAngle),0)
end
local LocalVelocity = RotationsOnlyWheelDirCFrame:ToObjectSpace(CFrame.new(carPrim:GetVelocityAtPosition(raycast.Position)))
local Xforce = RotationsOnlyWheelDirCFrame.RightVector * -LocalVelocity.x * wheelFriction
local Zforce = RotationsOnlyWheelDirCFrame.LookVector * seatPart.ThrottleFloat * torque * (math.sign(-LocalVelocity.z)==seatPart.Throttle and(1 - math.min(1,math.abs(LocalVelocity.z)/MaxSpeed))or 1)
SpringLengthMemory[wheelName] = SpringLength
carPrim:ApplyImpulseAtPosition(SuspensionForceVec3 + Xforce + Zforce, raycast.Position)
else
SpringLengthMemory[wheelName] = SuspensionMaxLength
end
end
if seatPart.Occupant ~= humanoid then--check whatever that means that character still sits in car
if seatPart.Parent.Name == "CarModel2" then
SuspensionMaxLength = 1.3
wheelRadius = 1.25
Stiffness = 118
Damper = 6
wheelFriction = 5.4
torque = 12.4
MaxSpeed = 80
SteerAngle = 20.74
SmoothSteer = 0
elseif seatPart.Parent.Name == "CarModel" then
SuspensionMaxLength = 1.3
wheelRadius = 1.25
Stiffness = 120.5
Damper = 6.4
wheelFriction = 5.5
torque = 6
MaxSpeed = 55
SteerAngle = 20
SmoothSteer = 0
end
serverTakeOverWait += delta
if serverTakeOverWait >= 0.5 then
break
end
end
end
end
end
end)
local WheelRotations = {}
game["Run Service"]:BindToRenderStep('MoveWheels',5,function()
for _,vehicle in pairs(workspace.Vehicles:GetChildren())do
local carPrim = vehicle.PrimaryPart
for i,v in pairs(WheelPositions)do
local carCFrame = carPrim.CFrame
local rayOrigin = carCFrame:ToWorldSpace(CFrame.new(Vector3.new(v.x,v.y - wheelRadius,v.z))).p
local rayDirection = -carCFrame.UpVector * SuspensionMaxLength
local rayParams = RaycastParams.new()
rayParams.FilterDescendantsInstances = {vehicle}
local raycast = workspace:Raycast(rayOrigin,rayDirection,rayParams)
if raycast then
local LocalVelocity = CFrame.lookAt(Vector3.zero,carCFrame.LookVector,carCFrame.UpVector):ToObjectSpace(CFrame.new(carPrim:GetVelocityAtPosition(raycast.Position)))
if not WheelRotations[vehicle] then
WheelRotations[vehicle] = {}
for i,v in pairs(WheelPositions)do WheelRotations[vehicle][i] = 0.5 end
end
WheelRotations[vehicle][i] += LocalVelocity.z/56
local DatWeld = vehicle.Wheels[i].Weld
DatWeld.C0 = CFrame.new(v - Vector3.new(0,(rayOrigin - raycast.Position).magnitude,0))
if string.sub(i,1,1)=='F' then
DatWeld.C0 = DatWeld.C0 * CFrame.Angles(0,-math.rad(SmoothSteer * SteerAngle),0)
end
if v.x>0 then
DatWeld.C0 = DatWeld.C0 * CFrame.Angles(0,math.rad(180),0)
DatWeld.C0 = DatWeld.C0 * CFrame.Angles(-WheelRotations[vehicle][i],0,0)
else
DatWeld.C0 = DatWeld.C0 * CFrame.Angles(WheelRotations[vehicle][i],0,0)
end
end
end
end
end)
Main Script:
local obj = script.Parent
local WheelPositions = {
FL = Vector3.new(-2.9,0,-5),
FR = Vector3.new(2.9,0,-5),
RL = Vector3.new(-2.9,0,5),
RR = Vector3.new(2.9,0,5),
}
local wheelModel = game.ReplicatedStorage.wheel
----------------------------
local seat = script.Parent.FL
local VehicleFolder = script.Parent.Parent
local AllCarsMemory = {}
local ExistingCarsTable = {}
local WheelFolder = Instance.new("Folder")
WheelFolder.Name = 'Wheels'
WheelFolder.Parent = script.Parent
for i,v in pairs(WheelPositions)do
local ClonedWheel = wheelModel:Clone()
ClonedWheel.Parent = WheelFolder
ClonedWheel.Name = i
ClonedWheel.CanCollide = false
local weld = Instance.new("Weld",ClonedWheel)
weld.Part0 = obj.PrimaryPart
weld.Part1 = ClonedWheel
weld.C0 = CFrame.new(WheelPositions[i])
if v.x>0 then weld.C0 = weld.C0 * CFrame.Angles(0,math. rad(180),0)end
end
local SuspensionMaxLength = 1.3
local wheelRadius = 1.25
local Stiffness = 118
local Damper = 6.4
local MaxSpeed = 80
local wheelFriction = 5.4
while true do
local delta = game["Run Service"].Heartbeat:Wait()
for _, carModel in pairs(ExistingCarsTable)do
local carPrim = carModel.PrimaryPart
if carPrim and not carPrim:GetNetworkOwner()then
local DisCarSpringLength = AllCarsMemory[carModel].SpringLengthMemory
for wheelName, originalPosition in pairs(WheelPositions)do
local carCFrame = carPrim.CFrame
local rayOrigin = carCFrame:ToWorldSpace(CFrame.new(originalPosition)).p
local rayDirection = -carCFrame.UpVector * (SuspensionMaxLength + wheelRadius)
local rayParams = RaycastParams.new()
rayParams.FilterDescendantsInstances = {carModel}
local raycast = workspace:Raycast(rayOrigin,rayDirection,rayParams)
if raycast then
local RaycastDistance = (rayOrigin - raycast.Position).magnitude
local SpringLength = math.clamp(RaycastDistance - wheelRadius, 0, SuspensionMaxLength)
local StiffnessForce = Stiffness * (SuspensionMaxLength - SpringLength)
local DamperForce = Damper * (( DisCarSpringLength[wheelName] - SpringLength) / delta)
local SuspensionForceVec3 = carCFrame.UpVector * (StiffnessForce + DamperForce)
local RotationsOnlyWheelDirCFrame = CFrame.lookAt(Vector3.zero,carCFrame.LookVector,carCFrame.UpVector)
local LocalVelocity = RotationsOnlyWheelDirCFrame:ToObjectSpace(CFrame.new(carPrim:GetVelocityAtPosition(raycast.Position)))
local Xforce = carCFrame.RightVector * -LocalVelocity.x * wheelFriction
local Zforce = carCFrame.LookVector * LocalVelocity.z * (wheelFriction/3)
DisCarSpringLength[wheelName] = SpringLength
carPrim:ApplyImpulseAtPosition(SuspensionForceVec3 + Xforce + Zforce, raycast.Position)
else
DisCarSpringLength[wheelName] = SuspensionMaxLength
end
end
end
end
end
seat.Changed:connect(function(p)
if p == "Occupant" then
local occupant = seat.Occupant
if occupant then
local player = game.Players:GetPlayerFromCharacter(occupant.Parent)
if player then
local localCarScript = script.CarLocalScript:Clone()
localCarScript.Parent = player.PlayerGui
localCarScript.Disabled = false
end
end
end
end)