You need to enable JavaScript to run this app. Roblox Studio门过场效果触发时出现Attempted to index nil报错
优惠活动
大模型
产品
解决方案
定价
更多

Roblox Studio门过场效果触发时出现Attempted to index nil报错

问题根因定位
  • 报错触发位置是代码第15行print(game.Players:GetPlayerFromCharacter(player).Name),报错本质是game.Players:GetPlayerFromCharacter()返回了nil,对空值索引.Name属性就会抛出对应错误。
  • 结合观察到的「进出一次门后再次触发就报错」的规律,核心问题有三点:
    1. 防抖(debounce)逻辑失效:Touched回调里设置debounce为true后,仅等待0.2秒就重置为false,但整套渐隐传送流程的实际执行时长至少为0.4秒(20帧淡入0.2s + 20帧淡出0.2s)。这导致过场动画还在执行的过程中,只要有任何部件碰到碰撞检测器(包括门自身的结构件、角色移动残留的碰撞触发、传送时带起的其他零件碰撞),就会再次触发函数执行,此时传入的hit.Parent根本不是有效的玩家角色,直接让GetPlayerFromCharacter返回nil触发报错。之前偶发正常是因为所有触发的碰撞对象都是有效玩家角色,一旦在防抖失效的窗口期碰到非玩家对象,就必然触发空索引错误。
    2. 缺少前置合法性校验:拿到碰撞事件的hit对象后,没有校验这个部件是否属于玩家角色,也没有校验GetPlayerFromCharacter的返回值是否有效,任何物体碰到碰撞块都会执行后续逻辑,一旦碰到非玩家对象就会触发空索引错误。
    3. 命名逻辑混淆:你在Touched回调里传给函数的参数是hit.Parent(即被碰到的零件所属的角色模型),但把这个参数命名为player,后续既把它当角色模型取HumanoidRootPart,又把它当Character实例传给GetPlayerFromCharacter,逻辑链没有兜底校验,很容易因为对象类型不匹配触发错误。
修复后代码
local doorModel = script.Parent.Parent
local afterIntoTransform = doorModel.DoorUnion.Position.Z - 6
local afterOutwardsTransform = doorModel.DoorUnion.Position.Z + 6
local debounce = false

local function executeFadeSceneAndTpPlayer(character)
    -- 校验角色有效性,获取对应Player对象
    local player = game.Players:GetPlayerFromCharacter(character)
    if not player then return end
    -- 校验角色根部件是否存在
    local rootPart = character:FindFirstChild("HumanoidRootPart")
    if not rootPart then return end

    local fadeScene = Instance.new("ScreenGui")
    fadeScene.Name = "fadeScene"
    fadeScene.ResetOnSpawn = false
    local fadeSceneFrame = Instance.new("Frame")
    fadeSceneFrame.Name = "fadeFrame"
    fadeSceneFrame.Size = UDim2.new(1,0,1,0)
    fadeSceneFrame.BorderSizePixel = 0
    fadeSceneFrame.BackgroundColor3 = Color3.new(1, 1, 1)
    fadeSceneFrame.BackgroundTransparency = 1
    fadeSceneFrame.Parent = fadeScene
    fadeScene.Parent = player.PlayerGui

    -- 淡入效果
    for i = 0, 20, 1 do
        fadeSceneFrame.BackgroundTransparency -= 0.05
        task.wait(0.01)
    end
    -- 传送玩家
    rootPart.Position = Vector3.new(rootPart.Position.X, rootPart.Position.Y, afterOutwardsTransform)
    -- 淡出效果
    for i = 0, 20, 1 do
        fadeSceneFrame.BackgroundTransparency += 0.05
        task.wait(0.01)
    end

    fadeScene:Destroy()
end

script.Parent.Touched:Connect(function(hit) 
    if debounce then return end
    -- 前置校验:碰到的对象是否为有效角色
    local character = hit.Parent
    if not character:FindFirstChildOfClass("Humanoid") then return end

    debounce = true
    executeFadeSceneAndTpPlayer(character)
    -- 等整套过场流程完全结束后再重置防抖
    task.wait(0.5)
    debounce = false
end)
关键改动说明
  • 调整防抖逻辑:把防抖的重置时机从过场流程中途移到流程完全结束后,避免动画执行过程中重复触发无意义的函数调用。
  • 增加两层前置校验:碰撞触发时先确认碰到的对象带有Humanoid组件,是有效角色;执行函数时再次确认能从角色拿到对应的Player对象、能找到RootPart,任何校验不通过就直接返回,从根源上杜绝空索引报错。
  • 修正命名歧义:把传入的角色模型参数改名为character,避免和玩家Player对象混淆,逻辑可读性和稳定性同步提升。
  • 给ScreenGui补充ResetOnSpawn = false配置,避免玩家重生时GUI被系统清除导致的后续引用丢失问题。
  • 把原有的wait()替换为Roblox推荐的task.wait(),调度精度和运行稳定性更高。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.11.24 16:54:03