You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

为何绑定Humanoid/Died事件的脚本仅触发一次?

问题:Boss死亡触发的胜利界面仅运行一次,重生脚本疑似引发问题

我有两个Roblox脚本分别存放在不同文件中,遇到了棘手的问题:第一个监听Boss死亡事件的脚本仅会运行一次,我已经通过打印测试确认了这个情况;第二个是给Humanoid/Noob模型添加的重生脚本,怀疑两者之间存在冲突。以下是我的代码:

Boss死亡触发胜利界面的脚本

game.Workspace.Demons_Boss.Humanoid.Died:connect(function() 
    for i, v in pairs(game.Players:GetChildren()) do 
        v.PlayerGui.ScreenGui.MagesWin.Visible = true 
        v.PlayerGui.ScreenGui.DemonsWin.Visible = false 
        v.PlayerGui.SreenGui.MagesWin.LocalScript.Disabled = false 
    end 
end)

重生脚本

name = "Humanoid" 
robo = script.Parent:Clone() 
While true do 
    wait(3) 
    if script.Parent.Humanoid.Health <1 then 
        robot = robo:Clone() 
        robot.Parent = script.Parent.Parent 
        robot:MakeJoints() 
        script.Parent:remove() 
        wait(7) 
        local p = game.Players:GetChildren() 
        for i = 1,#p do 
            p[i].Character.Head:remove() 
        end 
    end 
end

我已经花了一个月排查错误,急需技术帮助,谢谢!


问题分析与修复方案

核心问题定位

你遇到的「脚本仅运行一次」的根本原因是:第一个脚本只绑定了初始Boss的Died事件,当重生脚本删除原Boss并克隆新Boss后,新Boss没有绑定该事件,所以后续死亡不会触发胜利界面逻辑。此外两个脚本还存在语法错误和过时API的问题,我逐一帮你修复:

1. 修复Boss死亡事件脚本

主要解决事件绑定不持久、拼写错误、API过时三个问题:

-- Boss死亡事件脚本
local boss = game.Workspace.Demons_Boss
local function onBossDied()
    -- 用GetPlayers()替代GetChildren(),只返回玩家对象更安全
    for _, player in pairs(game.Players:GetPlayers()) do
        local playerGui = player:FindFirstChild("PlayerGui")
        if playerGui then
            local screenGui = playerGui:FindFirstChild("ScreenGui")
            if screenGui then
                local magesWin = screenGui:FindFirstChild("MagesWin")
                local demonsWin = screenGui:FindFirstChild("DemonsWin")
                if magesWin and demonsWin then
                    magesWin.Visible = true
                    demonsWin.Visible = false
                    -- 修正拼写错误:SreenGui → ScreenGui
                    local localScript = magesWin:FindFirstChild("LocalScript")
                    if localScript then
                        localScript.Disabled = false
                    end
                end
            end
        end
    end
end

-- 绑定初始Boss的死亡事件(用Connect替代过时的connect)
boss.Humanoid.Died:Connect(onBossDied)

-- 监听Workspace新增对象,为新克隆的Boss自动绑定死亡事件
game.Workspace.ChildAdded:Connect(function(child)
    if child.Name == "Demons_Boss" then
        -- 等待Humanoid加载完成再绑定事件
        local humanoid = child:WaitForChild("Humanoid")
        humanoid.Died:Connect(onBossDied)
    end
end)

2. 修复重生脚本

解决语法错误、API过时、变量不一致问题:

-- 重生脚本
local originalBoss = script.Parent
-- 确保原模型名字正确,方便后续识别新克隆的Boss
originalBoss.Name = "Demons_Boss"

-- 修正大小写:While → while(Lua大小写敏感)
while true do
    wait(3)
    local humanoid = originalBoss:FindFirstChild("Humanoid")
    -- 增加空值判断避免报错
    if humanoid and humanoid.Health < 1 then
        local newBoss = originalBoss:Clone()
        newBoss.Parent = originalBoss.Parent
        newBoss:MakeJoints()
        -- 用Destroy()替代过时的remove()
        originalBoss:Destroy()
        -- 更新originalBoss为新克隆的模型,让循环继续监听
        originalBoss = newBoss
        
        -- 优化玩家头部删除逻辑,增加空值判断
        local players = game.Players:GetPlayers()
        for _, player in pairs(players) do
            local character = player.Character
            if character then
                local head = character:FindFirstChild("Head")
                if head then
                    head:Destroy()
                end
            end
        end
        wait(7)
    end
end

关键说明

  • 新增的ChildAdded监听逻辑是解决「仅运行一次」的核心:每次重生脚本克隆新Boss后,这个监听会自动为新Boss绑定死亡事件,保证后续所有Boss死亡都会触发胜利界面。
  • Lua语法严格区分大小写(比如while不能写成While:Connect()不能写成:connect()),拼写错误(ScreenGui写成SreenGui)会直接导致脚本报错中断。
  • 使用FindFirstChild()WaitForChild()可以避免对象加载顺序问题导致的报错,让脚本更健壮。

内容的提问来源于stack exchange,提问作者Dang Khoa Chiem

火山引擎 最新活动