Attempt to index nil with 'Parent'

I am trying to make a Drivable vehicle for my game, I have the basics done, but I keep getting the Error " Workspace.Ute.Vehicle:5: attempt to index nil with ‘Parent’ " when hopping out of the vehicle and the remote wont fire.

Script Inside the Vehicle (One with the Error)

local RS = game:GetService("ReplicatedStorage")
local Players = game.Players

script.Parent.DriverSeat:GetPropertyChangedSignal("Occupant"):Connect(function()
	local PlayerLastInSeat = Players:GetPlayerFromCharacter(script.Parent.DriverSeat.Occupant.Parent)
	if script.Parent.DriverSeat.Occupant ~= nil then
		RS.VehicleSatEvent:FireClient(Players:GetPlayerFromCharacter(script.Parent.DriverSeat.Occupant.Parent), script.Parent)
	else
		RS.VehicleSatEvent:FireClient(PlayerLastInSeat, "Unbind")
	end
end)

The Local Script for controlling the Vehicle (Ignore the testing I was doing in there, its unfinnished)

local RS = game:GetService("ReplicatedStorage")
local CAS = game:GetService("ContextActionService")

local HH = nil

function Control(ActionName, inputState, inputObject)
	local Vehicle = HH
	if ActionName == "Foward" then	
		if inputState == Enum.UserInputState.Begin then
			local VectorForce = Instance.new("VectorForce")
			VectorForce.ApplyAtCenterOfMass = true
			VectorForce.Parent = Vehicle.ChasisMain
			VectorForce.Force = Vector3.new(100,0,0)
			
			print("W")
		elseif inputState == Enum.UserInputState.End then

		end

	elseif ActionName == "Backward" then	
		if inputState == Enum.UserInputState.Begin then
			Vehicle.TailLight0.Material = Enum.Material.Neon
			Vehicle.TailLight1.Material = Enum.Material.Neon
		elseif inputState == Enum.UserInputState.End then
			Vehicle.TailLight0.Material = Enum.Material.Glass
			Vehicle.TailLight1.Material = Enum.Material.Glass
		end
	elseif ActionName == "LeftTurn" then	
		if inputState == Enum.UserInputState.Begin then
			print("A")
		elseif inputState == Enum.UserInputState.End then

		end
	elseif ActionName == "RightTurn" then
		if inputState == Enum.UserInputState.Begin then
			print("D")
		elseif inputState == Enum.UserInputState.End then

		end
	elseif ActionName == "HeadLights" then	
		if inputState == Enum.UserInputState.Begin then
			print("Lights")
			if Vehicle.Headlight0.Material or Vehicle.Headlight1.Material == Enum.Material.Neon then

				Vehicle.Headlight0.Material = Enum.Material.Glass
				Vehicle.Headlight1.Material = Enum.Material.Glass
			else
				Vehicle.Headlight0.Material = Enum.Material.Neon
				Vehicle.Headlight1.Material = Enum.Material.Neon
			end
		end
	end
end


RS.VehicleSatEvent.OnClientEvent:Connect(function(Vehicle) 
	if Vehicle == "Unbind" then --Unbinds the Seat Controls (But other script wont fire the remote event)
		CAS:UnbindAction("Foward")
		CAS:UnbindAction("Backward")
		CAS:UnbindAction("LeftTurn")
		CAS:UnbindAction("RightTurn")
		CAS:UnbindAction("HeadLights")
	else
		HH = Vehicle
		CAS:BindAction("Foward", Control, false, Enum.KeyCode.W)
		CAS:BindAction("Backward", Control, false, Enum.KeyCode.S)
		CAS:BindAction("LeftTurn", Control, false, Enum.KeyCode.A)
		CAS:BindAction("RightTurn", Control, false, Enum.KeyCode.D)
		CAS:BindAction("HeadLights", Control, false, Enum.KeyCode.L)
	end
end)
local RS = game:GetService("ReplicatedStorage")
local Players = game.Players

script.Parent.DriverSeat:GetPropertyChangedSignal("Occupant"):Connect(function(Player)
	local PlayerLastInSeat = Players:GetPlayerFromCharacter(Player)
	if script.Parent.DriverSeat.Occupant ~= nil then
		RS.VehicleSatEvent:FireClient(Players:GetPlayerFromCharacter(script.Parent.DriverSeat.Occupant.Parent), script.Parent)
	else
		RS.VehicleSatEvent:FireClient(PlayerLastInSeat, "Unbind")
	end
end)```

I Tried this but it did not work, I got the error “FireClient: player argument must be a Player object”.

Ah, change the FireClient thing to PlayerLastInSeat.

Still getting the error, “FireClient: player argument must be a Player object”

just move this line outside of the function and define it whenever the occupant is not nil.

local RS = game:GetService("ReplicatedStorage")
local Players = game.Players

local PlayerLastInSeat 

script.Parent.DriverSeat:GetPropertyChangedSignal("Occupant"):Connect(function()
	if script.Parent.DriverSeat.Occupant ~= nil then
		PlayerLastInSeat = Players:GetPlayerFromCharacter(script.Parent.DriverSeat.Occupant.Parent)
		RS.VehicleSatEvent:FireClient(PlayerLastInSeat, script.Parent)
	else
		RS.VehicleSatEvent:FireClient(PlayerLastInSeat, "Unbind")
	end
end)

Also, I don’t know why you took the long way to get the character for the occupant isn’t nil fireclient so i changed it to playerlastinseat

1 Like