How can I fix my car spawner script?

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

Are there any errors in the script, and I’m assuming this is a local script, correct me if I’m wrong

This should not be done in the client, as only the player will be able to see the car, nobody else can (the car will only be spawned on the client, not on the server, thus the car will not be replicated to other clients.)

One more thing, assuming the provided code is the entire script, you still have not called spawnCar, or have not connected the function to an event (let’s say, TextButton.MouseButton1Click).

Regenerating a car must be server-side for players to see it (and the car must be in ServerStorage for security).