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

Roblox Studio中Proximity Prompt触发无响应问题求助

问题描述

设置的Proximity Prompt按住“E”时无任何反应,预期效果为显示店主并在聊天框打印玩家等级与货币,相关代码如下:

local Players = game:GetService("Players")

local shopMan = game.ReplicatedStorage.shopMan
local clonedShopMan = shopMan:Clone()
local username = Players.Name
local money = _G.publicPlayerSession.Money
local level = _G.publicPlayerSession.Level
local purchased = false

function loadData()
    
    if level == 1 then
        --new stand for free
    elseif level == 2 then
        --stand with another worker
    end
end

script.Parent.Triggered:Connect(function(player)
    print(money)
    print(level)
    if level > 0 then
        purchased = true
        loadData()
    else
        purchased = false
    end
    
    if purchased == false then
        if money >= 150 then
            money -= 150
            level += 1
            clonedShopMan.Parent = workspace.Stand
            script.Parent:Destroy()
        end
    end
end)

尝试过移除函数中的player参数、修改变量名等操作,问题依旧。


问题排查与修复

以下是代码中导致无反应的核心问题及修复方案:

1. 全局玩家数据引用错误

代码直接调用_G.publicPlayerSession.Money和_G.publicPlayerSession.Level,但全局变量publicPlayerSession未关联到触发ProximityPrompt的具体玩家,大概率为nil,导致后续level > 0判断不成立,打印内容为空。
修复:通过触发事件传入的player参数,获取该玩家的对应数据(需确保全局数据按玩家实例索引存储):

-- 在Triggered回调内替换原有money、level变量定义
local playerData = _G.publicPlayerSession[player]
local money = playerData and playerData.Money or 0
local level = playerData and playerData.Level or 0

2. 玩家名称获取错误

Players.Name是无效调用——Players是服务对象,没有Name属性,应使用触发事件的player参数获取玩家名称:

-- 将username定义移到Triggered回调内部
local username = player.Name

3. 克隆时机错误

提前克隆shopMan会导致多玩家触发时共用同一个克隆体,且初始克隆后未设置父级可能引发异常。应在确认需要显示店主时再克隆:

-- 删除提前克隆的代码,在需要创建店主的位置添加
local clonedShopMan = shopMan:Clone()
clonedShopMan.Parent = workspace.Stand

4. 基础设置检查

确认ProximityPrompt的Enabled属性为true,MaxActivationDistance设置合理,玩家在范围内按住“E”能触发事件。


修复后完整示例代码

local Players = game:GetService("Players")
local shopMan = game.ReplicatedStorage.shopMan

script.Parent.Triggered:Connect(function(player)
    -- 获取当前玩家的全局数据
    local playerData = _G.publicPlayerSession[player]
    if not playerData then
        warn("未找到玩家" .. player.Name .. "的会话数据")
        return
    end

    local money = playerData.Money
    local level = playerData.Level
    local purchased = false

    -- 打印玩家数据到控制台
    print(string.format("玩家%s:货币=%d,等级=%d", player.Name, money, level))

    if level > 0 then
        purchased = true
        -- 根据等级执行对应逻辑
        if level == 1 then
            -- new stand for free
        elseif level == 2 then
            -- stand with another worker
        end
    else
        purchased = false
    end

    if not purchased then
        if money >= 150 then
            -- 更新玩家数据
            playerData.Money -= 150
            playerData.Level += 1
            -- 创建店主实例
            local clonedShopMan = shopMan:Clone()
            clonedShopMan.Parent = workspace.Stand
            -- 销毁当前ProximityPrompt
            script.Parent:Destroy()
        end
    end
end)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 08:57:21