local xOffset = 2 --这个变量决定了玩家在离开座位后,玩家将传送到部件位置向左偏移多少后的位置(= 0将使玩家传送到部件位置)
--[[
注:
文中提到的部件皆指装载ProximityPrompt对象的这个部件。
玩家离开座位时将会传送到这个部件的坐标系(CFrame)的位置加上负xOffset后的位置。
请务必确保车辆座位的朝向与该部件的朝向一致,否则玩家传送后的位置与朝向会出现异常!
若发现玩家离开座位后被卡在车里,尝试将部件旋转180度,增大xOffset变量,并且确认部件是贴在车门上的!
]]
-------------------------------
local playersService = game:GetService("Players")
local marketPlaceService = game:GetService("MarketplaceService")
local userInputService = game:GetService("UserInputService")
local prompt = script.Parent
local part = prompt.Parent
local car = part.Parent.Parent
local gamePass = car:GetAttribute("gamePass")
local seat = car.DriveSeat
local currentOccupant
local playerFKeyStatus = {}
prompt.ActionText = "按F准备上车"
prompt.KeyboardKeyCode = Enum.KeyCode.F
local function onFKeyPressed(player)
playerFKeyStatus[player] = true
prompt.ActionText = "按E上车"
prompt.KeyboardKeyCode = Enum.KeyCode.E
end
local function onFKeyReleased(player)
end
local function sit(player:Player)
if not playerFKeyStatus[player] then
onFKeyPressed(player)
return
end
local canSit = false
if (not gamePass or (gamePass and gamePass == 0)) then
canSit = true
else
local isOwned = marketPlaceService:UserOwnsGamePassAsync(player.UserId,gamePass)
if not isOwned and _G.playerGamePassPurchased[gamePass] and table.find(_G.playerGamePassPurchased[gamePass],player) then
isOwned = true
end
if isOwned then
canSit = true
else
marketPlaceService:PromptGamePassPurchase(player,gamePass)
end
end
if canSit then
seat:Sit(player.Character.Humanoid)
playerFKeyStatus[player] = false
prompt.ActionText = "按F准备上车"
prompt.KeyboardKeyCode = Enum.KeyCode.F
end
end
local function onOccupantChanged()
local humanoid:Humanoid = seat.Occupant
if humanoid then
local player = playersService:GetPlayerFromCharacter(humanoid.Parent)
if gamePass and gamePass ~= 0 then
local isOwned = marketPlaceService:UserOwnsGamePassAsync(player.UserId,gamePass)
if not isOwned and _G.playerGamePassPurchased[gamePass] and table.find(_G.playerGamePassPurchased[gamePass],player) then
isOwned = true
end
if not isOwned then
humanoid.Jump = true
marketPlaceService:PromptGamePassPurchase(player,gamePass)
end
end
prompt.Enabled = false
currentOccupant = humanoid
else
local character:Model = currentOccupant.Parent
character:PivotTo(CFrame.new((part.CFrame * CFrame.new(-xOffset,0,0)).Position) * CFrame.Angles(seat.CFrame:ToEulerAnglesXYZ()))
character.PrimaryPart.AssemblyLinearVelocity = Vector3.new(0,0,0)
prompt.Enabled = true
if currentOccupant then
local player = playersService:GetPlayerFromCharacter(currentOccupant.Parent)
if player then
playerFKeyStatus[player] = false
prompt.ActionText = "按F准备上车"
prompt.KeyboardKeyCode = Enum.KeyCode.F
end
end
end
end
prompt.Triggered:Connect(sit)
seat:GetPropertyChangedSignal("Occupant"):Connect(onOccupantChanged)
playersService.PlayerRemoving:Connect(function(player)
playerFKeyStatus[player] = nil
end)
|