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

如何修复Roblox Studio中自定义玩家初始/最大生命值无效问题?

解决Roblox Studio中自定义玩家初始/最大生命值不生效的问题

你的两个脚本失效的原因:

  • 第一个脚本:PlayerAdded触发时玩家角色尚未加载完成,直接访问player.character会返回nil,无法找到Humanoid进行修改。
  • 第二个脚本:脚本放在StarterPlayerScripts中时,script.Parent指向的是脚本容器而非玩家角色,导致找不到Humanoid对象。

可行的解决方案:

方案1:通过PlayerAdded+CharacterAdded事件修改

将脚本放在ServerScriptService中(推荐使用服务器脚本),代码如下:

local DEFAULT_HEALTH = 10

game.Players.PlayerAdded:Connect(function(player)
    -- 等待角色实例创建完成
    player.CharacterAdded:Connect(function(character)
        local humanoid = character:WaitForChild("Humanoid")
        humanoid.MaxHealth = DEFAULT_HEALTH
        humanoid.Health = DEFAULT_HEALTH
    end)
end)

通过监听CharacterAdded事件,确保角色完全加载后再操作Humanoid,避免空引用问题。

方案2:将脚本放入StarterCharacterScripts

创建一个脚本,移动到StarterPlayer/StarterCharacterScripts目录下,代码如下:

local DEFAULT_HEALTH = 10

local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")

humanoid.MaxHealth = DEFAULT_HEALTH
humanoid.Health = DEFAULT_HEALTH

StarterCharacterScripts中的脚本会自动随角色生成复制到角色对象内,此时script.Parent就是玩家角色,能正确定位到Humanoid。

方案3:直接修改StarterPlayer属性(无需脚本)

在Studio的Explorer面板选中StarterPlayer,在右侧Properties面板找到HumanoidMaxHealth和HumanoidHealth,直接设置目标数值即可,所有玩家角色生成时会自动应用该设置。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 05:45:21