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

触碰Part打开专属商店菜单失败,报错Visible非PlayerGui有效成员

解决触碰Part打开专属商店菜单的报错问题

核心错误原因

你代码里的关键问题是拼写错误:把玩家对象下的PlayerGui写成了PlayersGui,多了一个字母s导致程序找不到对应成员,从而触发报错。

修正后的基础代码

local open = game.Workspace.openPart
local close = game.Workspace.closePart
local frame = script.Parent
frame.Visible = false

local function Shop(otherPart)
    -- 先判断触碰的是玩家角色,避免误触发
    local humanoid = otherPart.Parent:FindFirstChildOfClass("Humanoid")
    if not humanoid then return end
    
    -- 通过角色对象获取对应的玩家
    local player = game.Players:GetPlayerFromCharacter(otherPart.Parent)
    if not player then return end
    
    -- 修正拼写错误:PlayersGui → PlayerGui
    local shopGui = player.PlayerGui:FindFirstChild("Shop")
    if shopGui then
        shopGui.Frame.Visible = true
    end
end

open.Touched:Connect(Shop)

额外优化建议

  • 避免误触发:原代码直接通过otherPart.Parent.Name查找玩家,容易被场景内其他物件触发,加入Humanoid判断能确保只有玩家角色触发事件。
  • 确保GUI存在:如果你的Shop GUI资源不在玩家的PlayerGui内,建议在玩家加入游戏时自动复制GUI到其PlayerGui,可在ServerScriptService添加初始化脚本:
game.Players.PlayerAdded:Connect(function(player)
    local shopGui = script.Shop:Clone()
    shopGui.Parent = player.PlayerGui
end)
  • 补充关闭逻辑:你定义了closePart但未实现关闭功能,可补充以下代码:
local function CloseShop(otherPart)
    local humanoid = otherPart.Parent:FindFirstChildOfClass("Humanoid")
    if not humanoid then return end
    
    local player = game.Players:GetPlayerFromCharacter(otherPart.Parent)
    if not player then return end
    
    local shopGui = player.PlayerGui:FindFirstChild("Shop")
    if shopGui then
        shopGui.Frame.Visible = false
    end
end

close.Touched:Connect(CloseShop)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 01:32:00