I am trying to make a gui where when you click the button it spawns in a car and the player is sat in it, when you click it again the old car deletes and a new car spawns and the player is sat in it but right now it is not working, Legit nothing happens, How can I fix this?
-- Insert the car models you want to use here
local carModels = {
"Model1"
}
local player = game.Players.LocalPlayer
local currentCar = nil
-- Create the GUI
local gui = script.Parent.Parent
-- Create the button that spawns the car
local button = script.Parent
-- Function to spawn the car
local function spawnCar(carModelName)
-- Remove the old car if there was one
if currentCar then
currentCar:Destroy()
end
-- Create the new car and position it in front of the player
local carModel = game:GetService("ReplicatedStorage"):FindFirstChild(carModelName)
if not carModel then
warn("Car model not found:", carModelName)
return
end
local car = carModel:Clone()
car.Parent = workspace
-- Set the primary part of the car to the first child named "PrimaryPart" or the actual PrimaryPart if it exists
car.PrimaryPart = car:FindFirstChild("PrimaryPart") or car.PrimaryPart
car:SetPrimaryPartCFrame(player.Character.HumanoidRootPart.CFrame * CFrame.new(0, 0, -5))
-- Seat the player in the car
local seat = car:FindFirstChildOfClass("VehicleSeat")
if seat then
player.Character.Humanoid.Sit = true
seat:Sit(player.Character.Humanoid)
end
-- Set the current car to the new car
currentCar = car
end