Hello, I am hoping to make a car spawner that will spawn a car in front of the player.
Basically, I am making a storm-chasing game called ‘Aerial’ and it needs a car spawner for its storm-chasing cars.
The problem is I actually suck very bad at scripting, and I try to do something more erm - simple.
I put the car in ServerStorage, and in my car spawner script I make the car’s parent workspace, then its position the player.
But I had no luck, and it doesn’t work.
First, try putting the car in ReplicatedStorage instead of ServerStorage. If it doesn’t work, then try the following:
Place the car model in ReplicatedStorage;
Create a RemoteEvent in ReplicatedStorage named “SpawnCar”;
Create a new LocalScript on a TextButton located in StarterGui/ScreenGui;
The idea above is to create a button on the player’s screen that, when clicked, will spawn a new car.
In the LocalScript, add the following script:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SpawnCarEvent = ReplicatedStorage:WaitForChild("SpawnCar")
local Button = script.Parent
Button.MouseButton1Up:Connect(function()
SpawnCarEvent:FireServer()
end)
Call the SpawnCar event so that the server can spawn a new car.
In ServerScriptService, add a script that listens for the SpawnCar event and spawns a new car;"
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Car = ReplicatedStorage:WaitForChild("Car")
local SpawnCarEvent = ReplicatedStorage:WaitForChild("SpawnCar")
local SpawnDistance = 20
SpawnCarEvent.OnServerEvent:Connect(function(Player)
local Character = Player.Character
local Position = Character:FindFirstChild("HumanoidRootPart").Position
local Direction = Character:FindFirstChild("HumanoidRootPart").CFrame.LookVector
local CarSpawnPosition = Position + Direction * 20
local newCar = Car:Clone()
newCar.Parent = workspace
newCar:MoveTo(CarSpawnPosition)
end)
I hope this can help you, but remember, these scripts only spawn the car at a certain distance from the player without considering other factors like orientation and spawn time.