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

Attempt to index nil with 'VehicleSeat' How to fix?

Asked by 2 years ago
Edited 2 years ago

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)

1 answer

Log in to vote
0
Answered by
jundell 106
2 years ago
Edited 2 years ago

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)
0
Thank you for the Feedback, jundell. I will look into another way of writing it. kurtman247 2 — 2y
0
I have returned I did a couple of modifications to the script and it works just as how I imagined it to work, THANK YOU SO MUCH JUNDELL FOR THE HELP :D kurtman247 2 — 2y
Ad

Answer this question