Error: attempt to index nil with 'Name'

I am wishing to create some form of “Vehicle Spawner” that spawns the selected vehicle chosen from the Gui I made.

I get the error message, Attempt to index nil with 'Name'

Here is the code I’ve written:

This is where the “Spawn” button is clicked to “Spawn” the vehicle.

script.Parent.main.desc.one.button.spawnbutton.MouseButton1Click:Connect(function()
	local player = game.Players.LocalPlayer
	local SpawnPadPos = SpawnPadNumber.SpawnPos
	local vehicle = game.ReplicatedStorage.Vehicles.humvee
	SpawnEvent:FireServer(player, vehicle, SpawnPadPos)
end)

This is the script that functions on the ServerEvent.

game.ReplicatedStorage.SpawnEvents.SpawnEvent.OnServerEvent:Connect(function(player, vehicle, SpawnPadNumber, SpawnPadPos)
	print("Starting " ..player.Name.. "'s Vehicle Spawn")
	local vehicleAlreadyThere = game.Workspace:FindFirstChild(player.Name.. "'s Vehicle")

	if vehicleAlreadyThere then
		print(player.Name.. " Already has vehicle, Removing previous.")
		vehicleAlreadyThere:Destroy()
		SpawnCar(player, SpawnPadPos, vehicle)
	else
		print(player.Name.. " Does not already have a vehicle, spawning..")
		SpawnCar(player, SpawnPadPos, vehicle)
	end
end)

function SpawnCar(player, SpawnPadPos, vehicle)
	print(player.Name.. "'s Vehicle Spawning...")
	local CLONEDVehicle = vehicle:Clone()
	CLONEDVehicle.Name = player.Name.. "'s Vehicle"
	CLONEDVehicle.Parent = game.Workspace
	
	CLONEDVehicle:SetPrimaryPartCFrame(SpawnPadPos.PrimaryPart.CFrame)
	CLONEDVehicle:MakeJoints()
end

My result: ServerScriptService.vehicleSpawner:19: attempt to index nil with ‘Name’

Is there a solution to this, and where have I gone wrong in my code?

When you fire a remote event to the server the player event is a default parameter. So instead do

local vehicle = game.ReplicatedStorage.Vehicles.humvee
	SpawnEvent:FireServer(vehicle, SpawnPadPos)

The “attempt to index nil with name” most likely has to do with the player by default being nil, also when you do .OnServerEvent one of your parameters isn’t defined in the local script.

2 Likes

Works now, thanks for the help!