How to check if the seat.Occupant is the player who spawned car

local uis = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local EB = false

uis.InputBegan:Connect(function(input)
	if script.Parent.Roadster1.Equip.Value == true then
	if input.KeyCode == Enum.KeyCode.E and not EB then
		if player.Character.Humanoid.Health > 0 then
		EB = true
		player.Character.Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
		player.Character:TranslateBy(Vector3.new(0, 26, 0))
		game.ReplicatedStorage.CarCreate:FireServer()
		wait(10)
		EB = false
			end
		end
	end
end)

^ local script
(down arrow) server script

game.ReplicatedStorage.CarCreate.OnServerEvent:Connect(function(player)
	local model = game.ReplicatedStorage.Roadster4
	local backup = model:Clone()
	backup:SetPrimaryPartCFrame(player.Character.HumanoidRootPart.CFrame + Vector3.new(0, 25, 0))
	backup.Parent = game.Workspace
	backup:MakeJoints()
	wait(10)
	while true do
		wait(0.5)
		if not backup.VehicleSeat.Occupant then
			backup:Destroy()
			break
		end
	end
end)

i tried doing “if not backup.VehicleSeat.Occupant.Parent = player” and stuff like that but I’m not sure how to check it I’m basically doing this to prevent car stealing so if the occupant is not the player who spawned it then it deletes.

1 Like

Add the following event listener to the car.

backup.VehicleSeat:GetPropertyChangedSignal("Occupant"):Connect(function (humanoid)
    if humanoid and humanoid.Parent ~= player.Character then
        -- The person sitting in the seat is not the owner.
    end
end)
1 Like

This would result in the following code.

game.ReplicatedStorage.CarCreate.OnServerEvent:Connect(function(player)
	local model = game.ReplicatedStorage.Roadster4
	local backup = model:Clone()
	backup:SetPrimaryPartCFrame(player.Character.HumanoidRootPart.CFrame + Vector3.new(0, 25, 0))
	backup.Parent = game.Workspace
	backup:MakeJoints()
	backup.VehicleSeat:GetPropertyChangedSignal("Occupant"):Connect(function (humanoid)
		if humanoid and humanoid.Parent ~= player.Character then
			backup:Destroy()
			return
		end
	end)
end)