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

Roblox脚本报错:Touched is not a valid member of Model 求助

解决Roblox树木触碰倒下脚本报错问题

错误原因

报错Touched is not a valid member of Model "Workspace.Tree"是因为Model对象本身没有Touched事件,只有BasePart类的实例(比如Part、MeshPart等物理碰撞部件)才支持Touched事件。另外你原代码里直接给Model设置Anchored=false也是错误的——Model没有Anchored属性,需要给模型内的物理部件单独设置。

修复方案

方案1:指定模型内的核心碰撞部件(比如树干)

如果你的Tree模型里有一个专门的碰撞部件(比如命名为Trunk的MeshPart/Part),可以直接给这个部件绑定Touched事件,触发时遍历模型内所有部件解除锚定:

local treeModel = script.Parent
-- 找到模型里的树干碰撞部件
local trunkPart = treeModel:FindFirstChild("Trunk")

if not trunkPart or not trunkPart:IsA("BasePart") then
    warn("模型中未找到有效的树干碰撞部件")
    return
end

function onTreeTouched(hit)
    -- 检查触碰者是否是玩家角色
    if hit.Parent:FindFirstChild("Humanoid") then
        -- 遍历模型内所有部件,解除锚定
        for _, part in ipairs(treeModel:GetDescendants()) do
            if part:IsA("BasePart") then
                part.Anchored = false
            end
        end
        -- 只触发一次,避免重复触发
        trunkPart.Touched:Disconnect()
    end
end

trunkPart.Touched:Connect(onTreeTouched)

方案2:给模型内所有可碰撞部件绑定事件

如果模型里有多个需要检测触碰的部件,可以遍历所有BasePart绑定Touched事件:

local treeModel = script.Parent

function onPartTouched(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        -- 解除所有部件锚定
        for _, part in ipairs(treeModel:GetDescendants()) do
            if part:IsA("BasePart") then
                part.Anchored = false
            end
        end
        -- 断开所有部件的Touched事件,防止重复触发
        for _, part in ipairs(treeModel:GetDescendants()) do
            if part:IsA("BasePart") then
                part.Touched:Disconnect()
            end
        end
    end
end

-- 遍历模型内所有BasePart,绑定事件
for _, part in ipairs(treeModel:GetDescendants()) do
    if part:IsA("BasePart") then
        part.Touched:Connect(onPartTouched)
    end
end

关键注意点

  • 确保模型内的部件都设置了CanCollide=true,否则Touched事件不会触发。
  • 使用:Connect()而不是旧的:connect(),后者已被Roblox废弃。
  • 触发后断开事件可以避免重复执行解除锚定操作,提升性能。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 20:35:18