Car spawning script gives error

Hello. Whenever you go ahead and try to spawn a car, it gives an error that there’s an index error with :Clone().

Error:

Players.TheKman2019.PlayerGui.Civ_Car_Spawn.Main.VehicleInfo.Spawn.CarspawnScript:5: attempt to index nil with 'Clone' 

Script:

script.Parent.MouseButton1Up:Connect(function(player)
	
	local SelectedCar = game.ReplicatedStorage.CarSystem.SelectedCar.Value
	local Car = SelectedCar
	local CloneCar = Car:Clone()
	local Position1 = game.Workspace["Civilian Spawn"].CarSpots.S1.Position
	local Position2 = game.Workspace["Civilian Spawn"].CarSpots.S2.Position
	local Position3 = game.Workspace["Civilian Spawn"].CarSpots.S3.Position
	local Position4 = game.Workspace["Civilian Spawn"].CarSpots.S4.Position
	local Position5 = game.Workspace["Civilian Spawn"].CarSpots.S5.Position
	local PossibleSpawns = {Position1, Position2, Position3, Position4, Position5}
	
	local Car = SelectedCar
	if Car then
		
	else
		print("Car not selected")
	end
	

	CloneCar.Parent = game.Workspace
	CloneCar.PrimaryPart = CloneCar.Primary
	CloneCar:SetPrimaryPartCFrame(CFrame.new(PossibleSpawns[math.random(1, #PossibleSpawns)]))
	
	game.ReplicatedStorage.PlayerSpawnedCar:FireServer(player)
	script.Parent.Parent.Enabled = false
end)

You might be able to player put inside the brackets, as it would command “local CloneCar = Car:Clone(player)”

Unfortunately it didn’t work and I have the same error :frowning:

hi again it must be because the value sometimes is nil
(nevermind you did that code)
make it print the selected car value

Coming back as Nil, i’ll go bug hunting.

It’s saying Nil, but in the file explorer it isn’t.
image

is it client sided or serversided

Server side

Edit: If you set the value in the file explorer, it spawns that car.

i have an question tho why do you have 2 locals car and why is the selectedcar an server value cuz if another player clicks (just an example sorry) it will change but if you clicked after a few seconds you will get that player car (IF that is the fully script of the mousebutton1up)
but yeah thats weird

1 Like

image
did you fix this cuz you cannot firesrver in a serversided script

1 Like

The value is set from the client and read from the server. I really don’t know how you can spawn a car on the client side because then the car would be invisible to all other players no?

ok then you should aswell fire the car so that it uhh
appears for others if it can find?
but yeah you should rescript so that in the client when you mouse1up thingie you fire the selectedcarvalue and the serverscript will do its stuff
i can show you an example if you didnt get what i said

1 Like

That was something for testing, you can ignore that part.

ok but did you update the mousebutton1up script
can you send it here if something changed

1 Like

Sorry for the late response, here’s my progress so far. It’s giving an error for SetPrimaryPartCFrame which was never an error when this even worked in the past.

Client:

script.Parent.MouseButton1Up:Connect(function(player)
	local SelectedCar = game.ReplicatedStorage.CarSystem.SelectedCar.Value
	print(SelectedCar)

	game:GetService("ReplicatedStorage").VehicleSpawnRequest:FireServer(SelectedCar)
end)

Server:

local RS = game:GetService("ReplicatedStorage")
	

RS.VehicleSpawnRequest.OnServerEvent:Connect(function(SelectedCar, player)
	local Car = SelectedCar
	local CloneCar = Car:Clone(player)
	local Position1 = game.Workspace["Civilian Spawn"].CarSpots.S1.Position
	local Position2 = game.Workspace["Civilian Spawn"].CarSpots.S2.Position
	local Position3 = game.Workspace["Civilian Spawn"].CarSpots.S3.Position
	local Position4 = game.Workspace["Civilian Spawn"].CarSpots.S4.Position
	local Position5 = game.Workspace["Civilian Spawn"].CarSpots.S5.Position
	local PossibleSpawns = {Position1, Position2, Position3, Position4, Position5}
	
	if Car then
		
	else
		print("Car not selected")
	end
	

	CloneCar.Parent = game.Workspace.Vehicles
	CloneCar:SetPrimaryPartCFrame(CFrame.new(PossibleSpawns[math.random(1, #PossibleSpawns)]))
end)

All cars have a primary part set.

here you have a few issues, onserverevent first is the (plr,selectedcar) but if you want that way
and second why do you use :Clone(player)

1 Like

:Clone(player) was fixed before. I’ll test some other things as soon as my account gets unbanned because of course, moderation falsely moderated me.

It’s impossible to clone a value’s value

It’s not a string value, it’s an object value which makes it possible to interact with.

There is no other answer than just SelectedCar is nil.

Is the button script on the server (script), but you changed the value/selection on the client (local script)?

If so, it’s not replicating and thus leaving CarSystem.SelectedCar with a value of nil. You can check this by selecting a car and then swapping the view to server via the Test ribbon tab.

Also, if this button script is a normal server script, then not much else in the code will work.