Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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)

2 comments

0

Thank you for the Feedback, jundell. I will look into another way of writing it.

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

This discussion is preserved as it was. New answers, comments, and votes are closed.