为了实现玩家按下E键上车时不取消准备状态,您需要在`onFKeyPressed`函数中保留对F键的逻辑,但确保按下E键时只执行上车的功能,而不改变按F键的准备状态。以下是修改后的代码:
local xOffset = 2 -- 这个变量决定了玩家在离开座位后,玩家将传送到部件位置向左偏移多少后的位置(= 0将使玩家传送到部件位置)
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 = false -- 存储玩家按键状态(是否准备上车)
prompt.ActionText = "按F准备上车" -- 提示文本
prompt.KeyboardKeyCode = Enum.KeyCode.F -- 对应的按键代码
-- 当F键被按下时的处理函数
local function onFKeyPressed(player)
if playerFKeyStatus then
-- 如果已经准备上车,再次按下F取消准备
playerFKeyStatus = false
prompt.ActionText = "按F准备上车" -- 恢复提示文本
prompt.KeyboardKeyCode = Enum.KeyCode.F -- 恢复按键代码
else
playerFKeyStatus = true -- 设置F键状态为已按下
prompt.ActionText = "按F取消准备上车" -- 更新提示文本为取消准备
prompt.KeyboardKeyCode = Enum.KeyCode.F -- 更新按键代码为F
end
end
-- 玩家尝试坐下的功能
local function sit(player:Player)
if not playerFKeyStatus then
onFKeyPressed(player) -- 如果F键没有被按下则执行按下处理
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 and table.find(_G.playerGamePassPurchased, 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 = false -- 重置F键状态
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 and table.find(_G.playerGamePassPurchased, 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 = false -- 重置F键状态
prompt.ActionText = "按F准备上车" -- 恢复提示文本
prompt.KeyboardKeyCode = Enum.KeyCode.F -- 恢复按键代码
end
end
end
end
prompt.Triggered:Connect(sit) -- 连接提示框的触发事件到坐下函数
seat:GetPropertyChangedSignal("Occupant"):Connect(onOccupantChanged) -- 连接座位占用者变化信号
-- 当玩家按下键盘时
userInputService.InputBegan:Connect(function(input, gameProcessedEvent)
if not gameProcessedEvent and input.KeyCode == Enum.KeyCode.F then
local player = playersService.LocalPlayer -- 获取当前玩家
onFKeyPressed(player) -- 调用按键处理函数
end
if not gameProcessedEvent and input.KeyCode == Enum.KeyCode.E then
local player = playersService.LocalPlayer -- 获取当前玩家
sit(player) -- 调用坐下功能
end
end)
-- 当玩家离开时重置状态
playersService.PlayerRemoving:Connect(function(player)
playerFKeyStatus = nil -- 清空玩家按键状态
end)
### 修改内容说明:
1. **F键的逻辑**:`onFKeyPressed`函数现在会处理玩家按下F键以准备上车和取消准备的逻辑。
- 如果`playerFKeyStatus`为`true`,表示玩家已准备上车,再按一次F键会取消准备,并将提示文本重置为“按F准备上车”。
- 如果`playerFKeyStatus`为`false`,表示玩家尚未准备上车,再按一次F键会将提示文本更新为“按F取消准备上车”。
2. **E键的逻辑**:在`userInputService.InputBegan`事件的处理程序中添加了对E键的检测。
- 当玩家按下E键时会调用`sit(player)`函数,尝试让玩家坐下,且不会影响F键的状态。
通过这种方式,玩家可以在按下E键时顺利上车,而F键的准备状态不会被影响,实现了功能的独立性。 |