You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Roblox背包系统脚本问题求助:点击无响应且无报错

问题分析与修复方案

核心问题点

  • 事件监听逻辑错误:Equipped和Unequipped是工具对象的事件,不能用if直接判断状态,必须通过:Connect()绑定回调函数来更新装备状态。
  • UIS回调参数顺序错误:InputBegan的回调参数顺序应为input在前、gameProcessedEvent在后,参数顺序颠倒会导致输入判断逻辑失效。
  • UI对象引用错误:直接读取game.StarterGui内的对象是错误的,StarterGui中的内容会复制到每个玩家的PlayerGui中,需从玩家本地的PlayerGui获取目标对象。

修复后的代码

local Players = game:GetService("Players")
local UIS = game:GetService("UserInputService")

local player = Players.LocalPlayer
local playerGui = player.PlayerGui
-- 从玩家本地的PlayerGui中获取Collect对象
local collect = playerGui.Backpack.LocalScript.Collect
local rod = script.Parent
local equipped = false

-- 绑定装备事件,更新状态
rod.Equipped:Connect(function()
    equipped = true
end)

-- 绑定卸载事件,更新状态
rod.Unequipped:Connect(function()
    equipped = false
end)

-- 修正InputBegan参数顺序,增加系统输入过滤
UIS.InputBegan:Connect(function(input, gameProcessedEvent)
    -- 忽略系统已处理的输入(比如聊天框内点击)
    if gameProcessedEvent then return end
    if input.UserInputType == Enum.UserInputType.MouseButton1 and equipped then
        collect.Value = collect.Value + 1
    end
end)

额外说明

  • 加入gameProcessedEvent判断是为了避免玩家在使用聊天框、系统菜单时误触发道具逻辑,提升操作体验。
  • 确保Collect是IntValue或同类数值类型对象,否则无法执行+1操作。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 02:57:04