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

如何在非玩家Humanoid死亡时将服务器内所有玩家传送到指定Part?

Roblox NPC死亡触发全体玩家传送实现方案

原代码问题说明

直接用if ghost.Zombie.Died的写法是错误的,Died是Humanoid的事件,需要通过:Connect()方法绑定回调函数,才能在NPC死亡时触发后续逻辑。

完整实现代码

-- 定位NPC和传送目标点
local ghostNpc = game.Workspace[" "] -- 你的NPC所在容器
local teleportTarget = game.Workspace.Tele2 -- 传送目标Part

-- 获取NPC的Humanoid组件(对应你使用的Ghost NPC模型)
local npcHumanoid = ghostNpc.Zombie:FindFirstChildOfClass("Humanoid")
if not npcHumanoid then
    warn("未找到NPC的Humanoid组件")
    return
end

-- 监听NPC死亡事件
npcHumanoid.Died:Connect(function()
    -- 遍历服务器内所有玩家
    for _, player in ipairs(game.Players:GetPlayers()) do
        -- 确保玩家角色存在且有HumanoidRootPart
        local character = player.Character
        if character and character:FindFirstChild("HumanoidRootPart") then
            -- 传送玩家到目标点,添加随机偏移避免玩家重叠
            character.HumanoidRootPart.CFrame = teleportTarget.CFrame * CFrame.new(math.random(-2,2), 0, math.random(-2,2))
        end
    end
end)

关键逻辑解释

  • 事件绑定:通过npcHumanoid.Died:Connect()监听NPC死亡,只有当NPC的Humanoid生命值归0时,才会执行回调里的传送逻辑。
  • 玩家遍历:用game.Players:GetPlayers()获取当前服务器所有在线玩家,逐个处理传送。
  • 安全判断:先检查玩家角色是否存在、是否有HumanoidRootPart,避免运行报错。
  • 位置偏移:传送时添加随机小偏移,防止多个玩家重叠在同一个坐标点。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 20:37:10