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

Roblox Lua脚本报错:attempt to index nil with 'CFrame' 求助

问题排查与解决

报错原因分析

报错attempt to index nil with 'CFrame'说明变量TeleportPoint是nil,也就是game.Workspace.TeleportPoints:FindFirstChild(Position)没有找到对应的传送点对象,直接访问其CFrame属性才触发了错误。

具体排查与修复步骤

  • 核对传送点容器与对象
    确认Workspace下存在名为TeleportPoints的容器,且容器内有与Position参数完全匹配的对象(注意名称大小写敏感)。比如GUI传递的参数是"SpawnPoint",容器里必须有名为"SpawnPoint"的Part或Model。
  • 验证客户端传递的参数
    在服务端脚本中添加日志,确认客户端触发事件时传递的Position参数是否正确:
    print("收到的传送点名称:", Position)
    
  • 添加空值判断,避免崩溃
    在访问属性前,先检查玩家角色、HumanoidRootPart以及传送点是否存在,修改后的脚本如下:
    local TeleportEvent = game.ReplicatedStorage.TeleportEvent
    
    TeleportEvent.OnServerEvent:Connect(function(Player, Position)
        -- 检查玩家角色是否加载完成
        if not Player.Character then return end
        local humanoidRootPart = Player.Character:FindFirstChild("HumanoidRootPart")
        if not humanoidRootPart then return end
    
        local TeleportPoint = game.Workspace.TeleportPoints:FindFirstChild(Position)
        -- 检查传送点是否存在
        if not TeleportPoint then
            print("不存在该传送点:", Position)
            return
        end
    
        -- 处理不同类型的传送点对象
        local targetCFrame
        if TeleportPoint:IsA("Model") then
            -- 如果是Model,确保已设置PrimaryPart
            targetCFrame = TeleportPoint.PrimaryPart and TeleportPoint.PrimaryPart.CFrame
        else
            targetCFrame = TeleportPoint.CFrame
        end
    
        if targetCFrame then
            humanoidRootPart.CFrame = targetCFrame
        end
    end)
    
  • 确认传送点对象类型
    如果传送点是Model,需要先设置其PrimaryPart,否则无法通过PrimaryPart.CFrame获取位置;如果是Part或BasePart,则可以直接访问CFrame属性。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 15:48:30