Загрузка данных


local UserInputService = game:GetService("UserInputService")
local seat = script.Parent
local isDriving = false

local spacePressedTime = 0
local isHandbrakeActive = false
local HOLD_DELAY = 0.25 

local function sendCommand(name, value)
	seat:SetAttribute(name, value)
	task.wait()
	seat:SetAttribute(name, nil)
end

seat:GetPropertyChangedSignal("Occupant"):Connect(function()
	if seat.Occupant and seat.Occupant.Parent == game.Players.LocalPlayer.Character then
		isDriving = true
		sendCommand("DriverEvent", "Sit")
	else
		if isDriving then
			isDriving = false
			sendCommand("DriverEvent", "Leave")
		end
	end
end)

UserInputService.InputBegan:Connect(function(input, processed)
	if processed or not isDriving then return end
	
	if input.KeyCode == Enum.KeyCode.Space then
		spacePressedTime = tick()
		isHandbrakeActive = false
		
		task.spawn(function()
			while UserInputService:IsKeyDown(Enum.KeyCode.Space) and isDriving do
				if tick() - spacePressedTime >= HOLD_DELAY then
					if not isHandbrakeActive then
						isHandbrakeActive = true
						seat:SetAttribute("Handbrake", true)
					end
				end
				task.wait()
			end
		end)
	elseif input.KeyCode == Enum.KeyCode.L then
		sendCommand("ToggleLights", true)
	elseif input.KeyCode == Enum.KeyCode.E then
		sendCommand("ToggleDriverDoor", true)
	elseif input.KeyCode == Enum.KeyCode.H then
		seat:SetAttribute("HornActive", true)
	end
end)

UserInputService.InputEnded:Connect(function(input)
	if not isDriving then return end
	if input.KeyCode == Enum.KeyCode.Space then
		local holdDuration = tick() - spacePressedTime
		
		if holdDuration < HOLD_DELAY then
			local character = game.Players.LocalPlayer.Character
			if character and character:FindFirstChild("Humanoid") then
				character.Humanoid.Sit = false
				task.wait(0.05)
				character.HumanoidRootPart.CFrame = character.HumanoidRootPart.CFrame * CFrame.new(-3, 2, 0)
			end
		else
			if isHandbrakeActive then
				seat:SetAttribute("Handbrake", false)
				isHandbrakeActive = false
			end
		end
	elseif input.KeyCode == Enum.KeyCode.H then
		seat:SetAttribute("HornActive", false)
	end
end) 
и 
local TweenService = game:GetService("TweenService")
local seat = script.Parent
local car = seat.Parent

local MAX_SPEED = seat:GetAttribute("MaxSpeed") or 60
local TURN_ANGLE = seat:GetAttribute("TurnAngle") or 25
local MAX_TORQUE = seat:GetAttribute("MaxTorque") or 60000

local isHandbrake = false
local lightsOn = false

local hl = car:FindFirstChild("Headlights") and car.Headlights:FindFirstChildOfClass("SpotLight")
local tl = car:FindFirstChild("TailLights") and car.TailLights:FindFirstChildOfClass("SpotLight")
local lb = car:FindFirstChild("LeftBlinker") and car.LeftBlinker:FindFirstChildOfClass("SpotLight")
local rb = car:FindFirstChild("RightBlinker") and car.RightBlinker:FindFirstChildOfClass("SpotLight")
local horn = seat:FindFirstChild("HornSound")

local doorStates = {LeftDoor = false, RightDoor = false, TrunkGate = false}
local cargoWelds = {}

if seat:FindFirstChild("RL_Hinge") then seat.RL_Hinge.MotorMaxTorque = MAX_TORQUE end
if seat:FindFirstChild("RR_Hinge") then seat.RR_Hinge.MotorMaxTorque = MAX_TORQUE end

-- 1. ДВИЖЕНИЕ И ПОВОРОТЫ КОЛЕС
seat:GetPropertyChangedSignal("Throttle"):Connect(function()
	local throttle = isHandbrake and 0 or seat.Throttle
	if seat:FindFirstChild("RL_Hinge") then seat.RL_Hinge.AngularVelocity = throttle * MAX_SPEED end
	if seat:FindFirstChild("RR_Hinge") then seat.RR_Hinge.AngularVelocity = throttle * MAX_SPEED end
	if tl then tl.Enabled = (throttle < 0 or isHandbrake) end
end)

seat:GetPropertyChangedSignal("Steer"):Connect(function()
	local steer = seat.Steer
	if seat:FindFirstChild("FL_Hinge") then seat.FL_Hinge.TargetAngle = steer * TURN_ANGLE end
	if seat:FindFirstChild("FR_Hinge") then seat.FR_Hinge.TargetAngle = steer * TURN_ANGLE end
	if lb then lb.Enabled = (steer < 0) end
	if rb then rb.Enabled = (steer > 0) end
end)

-- 2. ВРАЩЕНИЕ ДВЕРЕЙ И БАГАЖНИКА (SERVO)
local function handleInteraction(part)
	local hinge = part:FindFirstChild("DoorHinge") or part:FindFirstChild("GateHinge")
	if not hinge then return end
	
	local name = part.Name
	doorStates[name] = not doorStates[name]
	
	if doorStates[name] then
		hinge.TargetAngle = (name == "TrunkGate") and 90 or 75
	else
		hinge.TargetAngle = 0
	end
end

for _, part in pairs(car:GetChildren()) do
	local clickDetector = part:FindFirstChild("InteractClick")
	if clickDetector and clickDetector:IsA("ClickDetector") then
		clickDetector.MouseClick:Connect(function(player)
			if part.Name == "TrunkGate" and seat.Occupant and seat.Occupant.Parent == player.Character then
				return 
			end
			handleInteraction(part)
		end)
	end
end

-- 3. АВТО-СВАРКА ГРУЗА В БАГАЖНИКЕ
local function manageCargo(weldMode)
	if not car:FindFirstChild("TrunkArea") then return end
	if weldMode then
		local overlapParams = OverlapParams.new()
		overlapParams.FilterType = Enum.RaycastFilterType.Exclude
		overlapParams.FilterDescendantsInstances = {car}
		local partsInTrunk = workspace:GetPartsInPart(car.TrunkArea, overlapParams)
		
		for _, part in pairs(partsInTrunk) do
			if not part.Anchored and part.CanCollide then
				local weld = Instance.new("WeldConstraint")
				weld.Part0 = car.TrunkArea
				weld.Part1 = part
				weld.Parent = car.TrunkArea
				table.insert(cargoWelds, weld)
			end
		end
	else
		for _, weld in pairs(cargoWelds) do
			if weld and weld.Parent then weld:Destroy() end
		end
		cargoWelds = {}
	end
end

-- 4. ОБРАБОТКА ДЕЙСТВИЙ ВОДИТЕЛЯ
seat:GetAttributeChangedSignal("Handbrake"):Connect(function()
	isHandbrake = seat:GetAttribute("Handbrake") or false
	if isHandbrake then
		if seat:FindFirstChild("RL_Hinge") then seat.RL_Hinge.AngularVelocity = 0 end
		if seat:FindFirstChild("RR_Hinge") then seat.RR_Hinge.AngularVelocity = 0 end
		if tl then tl.Enabled = true end
	end
end)

seat:GetAttributeChangedSignal("ToggleLights"):Connect(function()
	if seat:GetAttribute("ToggleLights") then
		lightsOn = not lightsOn
		if hl then hl.Enabled = lightsOn end
	end
end)

seat:GetAttributeChangedSignal("ToggleDriverDoor"):Connect(function()
	if seat:GetAttribute("ToggleDriverDoor") then
		if car:FindFirstChild("LeftDoor") then handleInteraction(car.LeftDoor) end
	end
end)

seat:GetAttributeChangedSignal("DriverEvent"):Connect(function()
	local event = seat:GetAttribute("DriverEvent")
	if event == "Sit" then
		if car:FindFirstChild("LeftDoor") and not doorStates.LeftDoor then handleInteraction(car.LeftDoor) end
		manageCargo(true)
	elseif event == "Leave" then
		if car:FindFirstChild("LeftDoor") and not doorStates.LeftDoor then handleInteraction(car.LeftDoor) end
		manageCargo(false)
	end
end)

seat:GetAttributeChangedSignal("HornActive"):Connect(function()
	local isHornActive = seat:GetAttribute("HornActive") or false
	if horn then
		if isHornActive then
			if not horn.IsPlaying then horn:Play() end
		else
			horn:Stop()
		end
	end
end)