代码
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 = true
prompt.ActionText = "按E上车"
prompt.KeyboardKeyCode = Enum.KeyCode.E
end
local function onFKeyReleased(player)
end
local function sit(player:Player)
if not playerFKeyStatus 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 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
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
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 = nil
end) 以下是您提供的代码的详细注释和翻译:
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 = {} -- 存储玩家按键状态
prompt.ActionText = "按F准备上车" -- 提示文本
prompt.KeyboardKeyCode = Enum.KeyCode.F -- 对应的按键代码
-- 当F键被按下时的处理函数
local function onFKeyPressed(player)
playerFKeyStatus = true -- 设置F键状态为已按下
prompt.ActionText = "按E上车" -- 更新提示文本
prompt.KeyboardKeyCode = Enum.KeyCode.E -- 更新按键代码
end
-- 当F键释放时的处理函数
local function onFKeyReleased(player)
-- 此处可以添加释放键后的逻辑
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) -- 连接座位占用者变化信号
-- 当玩家离开时重置状态
playersService.PlayerRemoving:Connect(function(player)
playerFKeyStatus = nil -- 清空玩家按键状态
end)
### 脚本放置位置
根据代码结构,这段代码应该放在 `Server.lua` 中,因为它涉及玩家的游戏通行证和座位管理等服务器端操作。 再增加当玩家按F准备上车之后,再次按F取消 lizhuoda 发表于 2025-06-27 11:52
再增加当玩家按F准备上车之后,再次按F取消 ...
为了实现当玩家按下F键准备上车后,再次按下F键取消准备的功能,您需要在`onFKeyPressed`函数中添加逻辑来处理这种情况。以下是修改后的代码,包含了这些新功能的实现:
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 = "按E上车" -- 更新提示文本
prompt.KeyboardKeyCode = Enum.KeyCode.E -- 更新按键代码
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
end)
-- 当玩家离开时重置状态
playersService.PlayerRemoving:Connect(function(player)
playerFKeyStatus = nil -- 清空玩家按键状态
end)
### 修改内容说明:
1. 在`onFKeyPressed`函数中添加了判断:
- 如果`playerFKeyStatus`为`true`,表示玩家已经准备上车,则将其设置为`false`并恢复提示文本。
- 如果`playerFKeyStatus`为`false`,表示玩家尚未准备上车,则将其设置为`true`并更新提示文本。
2. 在脚本底部添加了用于检测用户输入的事件`userInputService.InputBegan`,当玩家按下F键时,会调用`onFKeyPressed`函数。
这种方式让玩家能够通过再次按下F键来取消准备,从而提升了交互体验。 增加个再次按F取消准备上车提示文本 lizhuoda 发表于 2025-06-27 11:56
增加个再次按F取消准备上车提示文本
为了实现再次按F键可以取消准备上车的提示文本,您可以在`onFKeyPressed`函数中进一步修改提示文本以适应用户体验。以下是修改后的代码,增加了相关提示文本:
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
end)
-- 当玩家离开时重置状态
playersService.PlayerRemoving:Connect(function(player)
playerFKeyStatus = nil -- 清空玩家按键状态
end)
### 修改内容说明:
1. 当玩家首次按下F键时,`prompt.ActionText` 的文本设置为“按F取消准备上车”,表示玩家已准备好上车。
2. 当玩家再次按下F键时,`prompt.ActionText` 被恢复为“按F准备上车”,表示玩家取消了准备上车。
这样一来,玩家在准备上车和取消准备之间就有了明确的视觉反馈,有助于提升用户体验。 按E上车别取消 lizhuoda 发表于 2025-06-27 11:58
按E上车别取消
为了实现玩家按下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键的准备状态不会被影响,实现了功能的独立性。
页:
[1]