Hello. I have been making a vehicle GUI for a summer update for my game. I am bad at scripting so I don’t know what I should do. I tried looking up on devforum and youtube but no answers solve my question.
IMAGES OF THE GUI:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local Car1 = ReplicatedStorage:WaitForChild("Car1")--Replace Car1 with the car you want to spawn
script.Parent.MouseButton1Click:Connect(function()
local Player = Players.LocalPlayer
local HumanoidRootPart = Player:FindFirstChild("HumanoidRootPart")
local NewCar1 = Car1:Clone()
NewCar1.Parent = workspace
NewCar1.PrimaryPart.Position = HumanoidRootPart.Position + Vector3.new(7,0,0)--Change the 7 to how many studs away you want the car to be
end)
Put this script in the spawn button and follow the instructions in the script
Doing all of this on a LocalScript would not replicate the vehicle to the server.
You would need to use RemoteEvents for this as you would want to send a request to the server everytime a player wants to spawn a car.
You will need to start with a LocalScript, and ensure that the spawn button is actually a TextButton and not a TextBox, judging from the explorer screenshots.
LocalScript - parent it to the spawn button:
-- assuming that you've made a selection system for what car to spawn on click
local RS = game:GetService("ReplicatedStorage")
local spawnCarEvent = RS:WaitForChild("SpawnCar") -- make a RemoteEvent called SpawnCar
local spawnButton = script.Parent
local carToSpawn = "Car" -- name of the vehicle to spawn in ReplicatedStorage
local debounce = false
spawnButton.MouseButton1Click:Connect(function()
if debounce then return end
debounce = true
spawnCarEvent:FireServer(carToSpawn)
task.wait(.5)
debounce = false
end)
ServerScript:
local RS = game:GetService("ReplicatedStorage")
local carsFolder = RS:WaitForChild("Cars") -- make a new folder called Cars
local spawnCarEvent = RS:WaitForChild("SpawnCar")
spawnCarEvent.OnServerEvent:Connect(function(plr, car)
local findCar = carsFolder:FindFirstChild(car)
local plrChar = plr.Character
if not findCar or not plrChar then return end
local spawnedCar = findCar:Clone()
spawnedCar.Parent = workspace
spawnedCar.PrimaryPart.CFrame = plrChar.HumanoidRootPart.CFrame * CFrame.new(5,0,0)
end)
I’d highly recommend adding a way to detect whether it’s a vehicle or not, such as a cars folder, as event manipulation can be used to spawn other items from ReplicatedStorage. Once this system gets more advanced and you add prices for the vehicles, I’d run checks on the server to ensure the player has enough as this can be manipulated if you solely rely on the client.