Server Script won't clone a model from ReplicatedStorage

Hey! I am working on a car spawning system and I’ve run into an issue: When I press spawn the output tells me: ServerScriptService.Inventory:19: attempt to index nil with 'Clone'.

This is the localscript that triggers the spawning:

local rs = game:GetService("ReplicatedStorage")
local Cars = rs:FindFirstChild("Cars")
local event = rs:WaitForChild("Spawn")

script.Parent.MouseButton1Down:Connect(function()
	local car = script.Parent.Parent.Car.Value
	local purchased = script.Parent.Parent.Parent.Parent.Parent.Parent.Purchases:FindFirstChild(car)
	
	if purchased.Value == true then
		local vehicle = script.Parent.Parent.Car.Value
		script.Parent.Parent.Parent.Parent.SpawnLoc.Value = script.Parent.Parent.Parent.Parent.Parent.Parent.PlayerScripts.ShowSpawnGUI.SpawnPlace.Value
		event:FireServer(vehicle)
	end
end)

That script runs fine.

This is the script that is supposed to spawn the car:

local rs = game:GetService("ReplicatedStorage")
local spawnevent = rs:WaitForChild("Spawn")

function SpawnCar(vehicle)
	local cartospawn = rs:FindFirstChild(vehicle):Clone() --Here's the issue
	cartospawn:SetPrimaryPartCFrame(player.PlayerGui.Spawn.SpawnLoc.Value.CFrame + Vector3.new(0, 20, 0))
	cartospawn.Parent = workspace
	cartospawn:MakeJoints()
end

spawnevent.OnServerEvent:Connect(SpawnCar)

Any ideas what the issue might be?

Thanks!

I replaced function SpawnCar(vehicle) with function SpawnCar(Player, vehicle) but unfortunately I get the same error.

FindFirstChild is not guaranteed to return an Instance; it’ll return nil if the instance does not exist (rather than erroring like indexing directly would). If you want to actually use FindFirstChild properly, you should check if it returned an actual value before trying to do anything with that value. For example:

local carToSpawn = rs:FindFirstChild(vehicle)
if carToSpawn then
	carToSpawn:Clone() -- and so on
else
	print(vehicle, "is not a valid vehicle") -- now u can figure out *what* string you're sending that isn't actually a vehicle
end
3 Likes

I tried this and it seems that HawkSix was right: The error was bruceywayne is not a valid vehicle. After adding the player as the first parameter like he suggested again, I got ArmyTruck is not a valid vehicle. After checking the script it seems that I was trying to get the model(which is in a folder) directly from ReplicatedStorage. I fixed that and now It tells me attempt to index nil with 'CFrame'. I’ve used SetPrimaryPartCFrame(CFrame + Vector3) in a server script before and I haven’t gotten this error. What could be the issue now?

1 Like

Can you print what this is?

It seems like you’re setting the SpawnLoc’s Value on the client, so it appears as nil on the server.

3 Likes

After printing this from the server it seems that indeed it is nil. Thanks for pointing it out to me!

1 Like

When sending RemoteEvents to the server, there’s a default player argument already, so make sure to add a player argument before ‘vehicle’. It might be mistaking the vehicle for the player. In other words, try SpawnCar(player, vehicle)

1 Like

Ensure vehicle is a string (or use vehicle.Name if Instance)
Optional: Use WaitForChild() instead of FindFirstChild
Ensure Archivable is enabled on the model

1 Like

After solving the “vehicle” issue I sent the spawn place as a parameter as well and now the system works perfectly! Thanks to everyone who replied!

1 Like