Car not working when spawned in using script?
When its in the workspace, it works perfectly. When I spawn it in using a script, it doesn't work. I have the car in the serverstorage, in a "Cars" folder.
002 | local ServerStorage = game:GetService( "ServerStorage" ) |
003 | local Car = ServerStorage.Cars.Car |
004 | local vehicleSeat = Car.Base.VehicleSeat |
006 | local motorFR = Car.WheelFR:FindFirstChildWhichIsA( "CylindricalConstraint" , true ) |
007 | local motorFL = Car.WheelFL:FindFirstChildWhichIsA( "CylindricalConstraint" , true ) |
008 | local motorBR = Car.WheelBR:FindFirstChildWhichIsA( "CylindricalConstraint" , true ) |
009 | local motorBL = Car.WheelBL:FindFirstChildWhichIsA( "CylindricalConstraint" , true ) |
011 | local springFR = Car.WheelFR:FindFirstChildWhichIsA( "SpringConstraint" , true ) |
012 | local springFL = Car.WheelFL:FindFirstChildWhichIsA( "SpringConstraint" , true ) |
013 | local springBR = Car.WheelBR:FindFirstChildWhichIsA( "SpringConstraint" , true ) |
014 | local springBL = Car.WheelBL:FindFirstChildWhichIsA( "SpringConstraint" , true ) |
016 | local wheelHingeR = Car.WheelFR:FindFirstChildWhichIsA( "HingeConstraint" , true ) |
017 | local wheelHingeL = Car.WheelFL:FindFirstChildWhichIsA( "HingeConstraint" , true ) |
028 | local BRAKING_TORQUE = 8000 |
032 | local MAX_TURN_ANGLE = 30 |
042 | local function setMotorTorque(torque) |
043 | motorFR.MotorMaxTorque = torque |
044 | motorFL.MotorMaxTorque = torque |
045 | motorBR.MotorMaxTorque = torque |
046 | motorBL.MotorMaxTorque = torque |
050 | local function setMotorVelocity(vel) |
051 | motorFL.AngularVelocity = vel |
052 | motorBL.AngularVelocity = vel |
054 | motorFR.AngularVelocity = -vel |
055 | motorBR.AngularVelocity = -vel |
059 | local function getAverageVelocity() |
060 | local vFR = -motorFR.Attachment 1. WorldAxis:Dot(motorFR.Attachment 1. Parent.RotVelocity) |
061 | local vRR = -motorBR.Attachment 1. WorldAxis:Dot(motorBR.Attachment 1. Parent.RotVelocity) |
062 | local vFL = motorFL.Attachment 1. WorldAxis:Dot(motorFL.Attachment 1. Parent.RotVelocity) |
063 | local vRL = motorBL.Attachment 1. WorldAxis:Dot(motorBL.Attachment 1. Parent.RotVelocity) |
064 | return 0.25 * ( vFR + vFL + vRR + vRL ) |
073 | local steerFloat = vehicleSeat.SteerFloat |
074 | local throttle = vehicleSeat.ThrottleFloat |
077 | local turnAngle = steerFloat * MAX_TURN_ANGLE |
078 | wheelHingeR.TargetAngle = turnAngle |
079 | wheelHingeL.TargetAngle = turnAngle |
082 | local currentVel = getAverageVelocity() |
084 | local motorTorque = 0 |
087 | if math.abs(throttle) < 0.1 then |
091 | elseif math.abs(throttle * currentVel) > 0 then |
094 | local r = math.abs(currentVel) / MAX_SPEED |
096 | motorTorque = math.exp( - 3 * r * r ) * TORQUE * throttle * throttle |
097 | targetVel = math.sign(throttle) * 10000 |
101 | motorTorque = BRAKING_TORQUE * throttle * throttle |
105 | setMotorTorque(motorTorque) |
106 | setMotorVelocity(targetVel) |
Next Script
02 | local ServerStorage = game:GetService( "ServerStorage" ) |
03 | local ReplicatedStorage = game:GetService( "ReplicatedStorage" ) |
04 | local SpawnCarEvent = ReplicatedStorage:WaitForChild( "SpawnCar" ) |
05 | local DeleteCarEvent = ReplicatedStorage:WaitForChild( "DeleteCar" ) |
07 | SpawnCarEvent.OnServerEvent:Connect( function (player, carName) |
08 | local Car = ServerStorage:FindFirstChild( "Cars" ):FindFirstChild(carName) |
10 | local clonedCar = Car:Clone() |
11 | clonedCar.Name = player.Name .. 'sCar' |
12 | clonedCar.Parent = game.Workspace |
13 | clonedCar:MoveTo(player.Character.HumanoidRootPart.Position + player.Character.HumanoidRootPart.CFrame.LookVector * 15 ) |
17 | DeleteCarEvent.OnServerEvent:Connect( function (player, Car) |
23 | game.Players.PlayerRemoving:Connect( function (player) |
24 | local Car = game.Workspace:FindFirstChild(player.Name .. 'sCar' ) |
30 | game.Players.PlayerAdded:Connect( function (player) |
31 | player.CharacterAdded:Connect( function (character) |
32 | local Car = game.Workspace:FindFirstChild(player.Name .. 'sCar' ) |
I can provide the delete and call scripts if needed.