Help with this button script

I have this script made by the new ai

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local Workspace = game:GetService("Workspace")

-- Assuming there's a UI button named "SpawnCarButton" under StarterGui
local spawnCarButton = script.Parent:WaitForChild("SpawnCarButton")

-- Assuming cars are stored in ReplicatedStorage under a folder named "Cars"
local carsFolder = ReplicatedStorage:WaitForChild("Cars")

-- Function to clone and position the car in front of the player
local function spawnCarForPlayer(player)
	local character = player.Character or player.CharacterAdded:Wait()
	local humanoidRootPart = character:WaitForChild("HumanoidRootPart")

	-- Get a random car model from the Cars folder
	local cars = carsFolder:GetChildren()
	local randomIndex = math.random(1, #cars)
	local selectedCar = cars[randomIndex]:Clone()

	-- Position the car in front of the player
	local spawnPosition = humanoidRootPart.Position + (humanoidRootPart.CFrame.LookVector * 10)
	selectedCar.PrimaryPart = selectedCar.DriveSeat
	selectedCar:SetPrimaryPartCFrame(CFrame.new(spawnPosition))

	-- Parent the car to the workspace
	selectedCar.Parent = Workspace
    wait(15)
end

-- Connect the button click event to the spawn function
spawnCarButton.MouseButton1Click:Connect(function()
	local player = Players.LocalPlayer
	spawnCarForPlayer(player)
end)

When i press the ui button, it DOES spawn the car in front of me, but it is floating and all functionality of it is broken, i can not get in the driver’s seat. No clue if the AI did something wrong as I wouldnt know any better. If anyone knows how to fix this that would be highly appreciated

Here’s a video of what happens for context

Assuming this is a local script, the car isn’t recognized by the server when spawned. This is why entering the VehicleSeat doesn’t work (Unless CanTouch is turned off). If this is the case, I suggest using a regular script to spawn the car through RemoteEvents.

After spawning the car, you can switch to the server to confirm if the car is client-sided.

Hello, thank you for the reply
The script is NOT a LocalScript, it is a regular script inside of the ScreenGui for the button. CanTouch is not turned off.
I have no clue how to spawn the car through RemoteEvents
I spawned the car and switched to the server side and I can confirm I do not see the car, so it is client-sided and you are correct

It’s likely a LocalScript as it uses Players.LocalPlayer which cannot be used on a regular script. RemoteEvents are objects that make it possible for a LocalScript to communicate with a regular Script and vice versa. I recommend checking out the article about it.

Remote Events are called using “:FireServer()” on a LocalScript and are picked up by a Script with “.OnServerEvent”. Of course, you are free to watch tutorials on how they work.

My bad, it is indeed a local script. Thank you for the advice