Hello everyone! I am trying to create a script where if the car from behind hits the car in front, the car in front will gain +10 MaxSpeed and then after 5 seconds it will decrease by 2 every 2 seconds. However, when I try to run this script in Studio, It keeps giving me this message, " attempt to index nil with 'VehicleSeat' " Do you know how to fix this problem?
local currentMaxSpeed = Model.VehicleSeat.MaxSpeed local speedBoost = 10 local speedFallOff = -2
function collided(hit) if hit.Name=="RearDraftingPart" then
VehicleSeat.MaxSpeed = currentMaxSpeed + speedBoost wait(5) VehicleSeat.MaxSpeed = currentMaxSpeed + speedBoost + speedFallOff wait(2) VehicleSeat.MaxSpeed = currentMaxSpeed + speedBoost + speedFallOff + speedFallOff wait(2) VehicleSeat.MaxSpeed = currentMaxSpeed + speedBoost + speedFallOff + speedFallOff + speedFallOff wait(2) VehicleSeat.MaxSpeed = currentMaxSpeed + speedBoost + speedFallOff + speedFallOff + speedFallOff + speedFallOff wait(2) VehicleSeat.MaxSpeed = currentMaxSpeed else VehicleSeat.MaxSpeed = currentMaxSpeed
end end
script.Parent.Touched:connect(collided)
Model and VehicleSeat aren't variables... This script assumes that it is parented to Model. Also, this is such a hacky way to do this; once you get it working, it won't work well. I recommend looking into a different method.
local Model = script.Parent local VehicleSeat = Model.VehicleSeat local currentMaxSpeed = VehicleSeat.MaxSpeed local speedBoost = 10 local speedFallOff = -2 function collided(hit) if hit.Name=="RearDraftingPart" then VehicleSeat.MaxSpeed = currentMaxSpeed + speedBoost wait(5) VehicleSeat.MaxSpeed = currentMaxSpeed + speedBoost + speedFallOff wait(2) VehicleSeat.MaxSpeed = currentMaxSpeed + speedBoost + speedFallOff * 2 wait(2) VehicleSeat.MaxSpeed = currentMaxSpeed + speedBoost + speedFallOff * 3 wait(2) VehicleSeat.MaxSpeed = currentMaxSpeed + speedBoost + speedFallOff * 4 wait(2) VehicleSeat.MaxSpeed = currentMaxSpeed else VehicleSeat.MaxSpeed = currentMaxSpeed end end script.Parent.Touched:Connect(collided)