Customizable Vehicle Spawner

I’m considering making a customizable vehicle spawner for my game. It would allow things like lightbar, light color, radar, etc. to be customized with each vehicle spawn.

As the player selects different changes, I want them to be visible in a GUI or perhaps in a garage-like setting.

I’d also like there to be certain preset “packages”, like marked, unmarked, no lightbar, etc.

What is the best way to go about this, in terms of scripting, so that I can maintain an easily modular system (I’d like to add cars to it as we develop them), show the player what they’re creating in real time, all while maintaining a system that isn’t laggy for others?

2 Likes

I would personally handle this by having a “customization module” for each type of car, and inside the module specify functions that change specific parts of the car. If all cars share similar layouts, you could do the custom modules but if a specific type of customization isn’t specified in the module, use a default function so that you don’t have to redefine it for every car.

Example:

CarCustomization.ChangeThisThing(PlayerInputtedSetting1)

For your presets system, you could just have a function that will call each customization function with pre-specified arguments.

1 Like

I’m sorry, I’m having a stupid moment week
Could you explain this a bit more in-depth? Thank you!

Obviously having a localscript that handles the GUI code such as selecting and unselecting certain features, then some sort of “Confirm” button. Upon confirmation you would want to use a remotefunction to ask the server to setup the car they are requesting, so for example an unmarked police car you might just have to apply some sort of unmarked lights. Same goes for a marked car, just have the server apply that lightbar and whatever else is requested. Once the car is all setup based on what the user wanted then you would move it to said spawner. If you want to show it in real-time then just have all the vehicles in a location that can be replicated to the client and use a viewportframe to show the model being updated or position the camera in an area that is positioned to view the vehicle locally. Updates to the vehicle can be made locally aswell, just don’t forget to update them serverside for the actual spawn. Alternatively you could have some pre-made cars such as Unmarked sedan or Marked sedan. That might be a little easier for less experienced people but also makes it less modular.

1 Like

So for ViewportFrames, I would have to clone a model of the stock car, parent it to the camera, and run the customization functions in real time?
And would I be able to make a garage/dealership (à la Ultimate Driving) in which the vehicle changes would be shown live?

Well if the car is in the garage (you can forget viewportframes if you are doing it this way) like those games then you would just locally apply the lightbars to a cloned version of your vehicle. Then when they “Confirm” their package it will ask the server to apply them on the actual vehicle that will be spawned in (and removing the local copy of your car ofcourse).

Player chooses unmarked car > Locally clones base version of that car > Allows player to add/remove lightbars, decals etc. > Player press confirm

For the remote you could do something like this, essentially the first parameter is the type of car, whether its a sedan, a pickup truck or a van(lol?). Then a table of objects to add to that car.

VehicleCustomizationEvent:FireServer(CarType, {Lightbar1, decal2, rambar1})

Make sure the player is allowed to have these items (make sure they are a cop in this case?) > Apply the objects serverside to a cloned version of the base car (just like you did on the client for viewing purposes) > Parent the car to workspace and position it wherever you want (the garage or a spawn pad whatever you want really.)

Kinda a rough rundown, hope this helps though.

1 Like

I just have one more question: why use a RemoteFunction as opposed to a RemoteEvent for this?

You can use either, a remotefunction just returns back to the client. So you could notify the player with something like “Vehicle spawned!” after it successfully creates and spawns the vehicle. Or “Error spawning vehicle!” if it somehow fails. Not much of a difference other than the client knowing whether it was a success or not.

In my example i did

VehicleCustomizationEvent:FireServer(Blah blah)

for a remote function you would just do

local variable = VehicleCustomizationFunction:InvokeServer(Blah blah)
if variable == true then
      -- Notify?
else
      -- Failed to load vehicle: .. tostring(variable)
end

Depends what you want though

1 Like

I used a RemoteFunction for a team change GUI in my game, but it lagged when I used it. Are RemoteFunctions typically a longer wait than RemoteEvents?

If your game is optimized there shouldn’t really be a noticeable difference. Using a remote function all the client is doing is yielding till it gets a response which can take a little longer than a remote event (which doesn’t give any sort of response back.). If a team change is lagging due to a remote function then you should look at the code on the server and further optimize it.

1 Like

Awesome. I’ll try and update this thread if I manage to get it working – thank you so much for all the help!

1 Like

@kinkocat Do you recommend using OOP to construct the actual vehicles?

Really personal preference, if i were you i would create some sort of module script that handles vehicle customization. If the rest of your game is OOP then you might aswell do it for this, however, if this is the only thing that is OOP then don’t bother.

You could do:

CustomizationModule:ApplyLightbar(VehicleModel)
CustomizationModule:GetLightbarPosition(VehicleModel, LightbarType)

If you wanted to do OOP you would do something like

local Vehicle = VehicleModule.new(VehicleModel)
Vehicle:ApplyLightbar()
Vehicle:ToggleHeadlights()
1 Like