You need to enable JavaScript to run this app. Roblox开发中如何规避聊天输入含R内容时误触发角色重置逻辑
优惠活动
大模型
产品
解决方案
定价
更多

Roblox开发中如何规避聊天输入含R内容时误触发角色重置逻辑

异常触发核心原因
  • 原代码未识别输入焦点归属,物理按下R键时,无论焦点在3D游戏场景还是聊天输入框,都会触发移除头部的重置逻辑
  • 原代码未读取InputBegan回调的内置标识参数,无法区分全局场景输入与聊天框内的文本输入事件
  • 原代码的角色引用在初始化时固定绑定初始角色,角色重生后旧引用会失效,后续触发逻辑可能出现报错
可直接复用的修复代码
local player = game:GetService("Players").LocalPlayer
local userInputService = game:GetService("UserInputService")
local resetCooldown = true
local currentCharacter = player.Character or player.CharacterAdded:Wait()

-- 同步角色重生后的最新引用
player.CharacterAdded:Connect(function(newCharacter)
    currentCharacter = newCharacter
end)

userInputService.InputBegan:Connect(function(input, gameProcessedEvent)
    -- 被聊天框等游戏内置UI处理的输入直接跳过
    if gameProcessedEvent then
        return
    end
    -- 保留原有的重置逻辑与冷却机制
    if input.KeyCode == Enum.KeyCode.R and resetCooldown then
        currentCharacter.Head:Remove()
        resetCooldown = false
        task.wait(6)
        resetCooldown = true
    end
end)
修复后实际效果
  • 打开聊天界面输入r字符时,输入事件被聊天UI捕获,gameProcessedEvent标识为true,直接跳过重置逻辑,不会触发角色反复死亡
  • 聊天框关闭、游戏场景获得输入焦点时,按下R键才会触发带6秒冷却的角色重置逻辑

内容的提问来源于stack exchange,提问作者user14461945

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.21 15:54:07