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

Roblox部件触碰脚本仅执行一次,需每次触发的原因咨询

问题原因分析

你这段Roblox脚本看似只执行一次,核心原因有两种可能:

  • UI状态无变化导致视觉误判
    第一次触碰后,upd界面的Enabled属性已经被设为true,后续每次触发Touched事件时,执行的还是plr.PlayerGui.upd.Enabled = true——这个操作不会改变UI的当前状态,所以你看不到任何新反馈,误以为脚本没触发,但实际上事件每次触碰(接触瞬间)都在正常执行。

  • Touched事件的触发逻辑特性
    Roblox的Touched事件仅在两个部件开始接触的瞬间触发一次,如果玩家一直站在部件上没有离开,事件不会重复触发,只有当玩家离开部件后再次触碰时,才会再次触发。如果你的需求是玩家持续接触时也重复执行逻辑,那这个事件的特性就会让你觉得“只执行了一次”。

针对性解决建议

如果是第一种情况(需要每次触碰都有可见反馈),可以改成切换UI状态或者添加其他反馈,比如:

part = script.Parent
part.Touched:Connect(function(hit)
    local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
    if plr then
        local updGui = plr.PlayerGui.upd
        updGui.Enabled = not updGui.Enabled -- 每次触碰切换显示/隐藏状态
        -- 或者添加其他反馈,比如播放音效、弹出提示等
    end
end)

如果是第二种情况(需要持续接触时重复执行逻辑),可以结合TouchEnded事件和循环检测,比如:

part = script.Parent
local touchingPlayers = {}

part.Touched:Connect(function(hit)
    local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
    if plr and not touchingPlayers[plr] then
        touchingPlayers[plr] = true
        -- 开启循环,持续执行逻辑直到玩家离开
        task.spawn(function()
            while touchingPlayers[plr] do
                plr.PlayerGui.upd.Enabled = true
                task.wait(1) -- 控制重复执行的间隔
            end
        end)
    end
end)

part.TouchEnded:Connect(function(hit)
    local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
    if plr then
        touchingPlayers[plr] = nil
    end
end)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 16:04:59