Car Spawing Help

Hello! So I am not sure on how to work this but I have a remote event fire when someone clicks a gui and it spawns a car. But for some reason Idk How to do it like that.

Script:

local events = game.ReplicatedStorage.CarSpawnerEvents

events.SpawnCar.OnServerEvent:Connect(function(CarName, Addon1)

local clone = game.ReplicatedStorage.ViewPortCars.CarName:clone()

end)

Script in the local script:

local event = game.ReplicatedStorage.CarSpawnerEvents.SpawnCar

script.Parent.MouseButton1Click:Connect(function()

event:FireServer(script.Parent.Name, script.Parent.Parent.Ram.Value)

end)

It keeps saying something like CarName is not a vaild member or something.

Can you show us how the ViewPortCars contents look like in the Explorer?

In the Server script, after you clone the car, you’ll want to Parent it to the workspace.
clone.Parent = workspace

Then, you will wanna position it. You can do this with MoveTo(). Make sure before doing this that the car model has a PrimaryPart. To set its PrimaryPart, look in the model’s properties for where it says “PrimaryPart.” Then click that and then click whatever part inside the model you want to be its PrimaryPart. This will make it act as sort of the “core” of the model.

So once you’ve done that:
clone:MoveTo(position)

1 Like

Alright, 2 errors I mainly see are The RemoteEvents first parameter should be player and

Capital C for clone and CarName Should be in Brackets.

local events = game.ReplicatedStorage.CarSpawnerEvents

events.SpawnCar.OnServerEvent:Connect(function(plr, CarName, Addon1)

local clone = game.ReplicatedStorage.ViewPortCars[CarName]:Clone()

end)


I am getting that error I am not sure on how to fix it.

The problem is that the first parameter of an OnServerEvent is always “Player” as I said here.

Ah k I get it Ill try to work a way round it

You don’t have to work around it. Just add another parameter before CarName.

local events = game.ReplicatedStorage.CarSpawnerEvents

events.SpawnCar.OnServerEvent:Connect(function(plr, CarName, Addon1)

local clone = game.ReplicatedStorage.ViewPortCars[CarName]:Clone()

end)
1 Like

Yeah thats what I ment sorry. Thanks

Try this, your first script doesn’t have the player variable which is always the first variable on an OnServerEvent. Also, you are finding CarName inside the ViewPortCars, but you need to use a WaitForChild since you are finding for the CarName’s value, not the CarName word itself.

local events = game.ReplicatedStorage.CarSpawnerEvents

events.SpawnCar.OnServerEvent:Connect(function(player, CarName, Addon1)
   local clone = game.ReplicatedStorage.ViewPortCars:WaitForChild(CarName):Clone()
end)

Nvm someone already answered xD