Hello.
I want to create a script that only lets the owner of the car drive it when the car has been spawned in. I have found a script that allows for this to happen, but I have to manually set the owner of the vehicle before they can drive it.
I want to be able to add a part to the script that automatically changes the owner value when the user spawns the car in, but I don’t know how to do this.
The script I found is:
seat = script.Parent
owner = script.Parent.owner.Value
seat.Changed:Connect(function(plr)
if seat.Occupant then
if not seat.Occupand.Name == owner then
local humanoid = seat.Occupant
if humanoid then
local player = game:GetService("Players"):GetPlayerFromCharacter(humanoid.Parent)
if player then
seat:SetNetworkOwner(player)
seat.Occupant.Jump = true
end
end
end
end
end)
Change it to this where the script checks for the owner each time someone sits in it
Then edit the script for spawning in the car so that it changes the owner value in the car to the owners name
seat = script.Parent
owner = script.Parent.owner
seat.Changed:Connect(function(plr)
if seat.Occupant then
if not seat.Occupand.Name == owner.Value then
local humanoid = seat.Occupant
if humanoid then
local player = game:GetService("Players"):GetPlayerFromCharacter(humanoid.Parent)
if player then
seat:SetNetworkOwner(player)
seat.Occupant.Jump = true
end
end
end
end
end)
This is the script inside one of the buttons to spawn the car.
local player = game.Players.LocalPlayer
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SpawnCarEvent = game.ReplicatedStorage:WaitForChild("SpawnCar")
local DeleteCarEvent = game.ReplicatedStorage:WaitForChild("DeleteCar")
local CarParams = game.ReplicatedStorage:WaitForChild("GetCarParams"):InvokeServer(script.Parent.Name)
local carName = script.Parent.Name
local SpawnCarFrame = script.Parent.Parent
script.Parent.CarName.Text = CarParams.Name
script.Parent.MouseButton1Down:Connect(function()
SpawnCarFrame.Visible = false
local CurrentCar = game.Workspace:FindFirstChild(player.Name .. "sCar")
if not CurrentCar then
SpawnCarEvent:FireServer(carName)
else
for i, v in pairs(CurrentCar:GetDescendants()) do
if v:IsA("VehicleSeat") or v:IsA("Seat") then
if v.Occupant and v.Occupant:IsA("Humanoid") then
v.Occupant.Sit = false
end
end
end
wait()
DeleteCarEvent:FireServer(CurrentCar)
SpawnCarEvent:FireServer(carName)
end
end)