Roblox Studio 设备中的健康栏 GUI 问题

问题描述 投票:0回答:1

我在 Roblox Studio 中制作了一个健康栏,但我希望它能够在设备之间工作(因此它可以根据设备进行缩放),问题是,每次失去健康时它的大小都会减小,所以我无法使用 UIAspectRatioConstraint。我已经尝试了很多个小时,但找不到或尝试任何东西。任何帮助将非常感激。

脚本:

local Player = game.Players.LocalPlayer
local Character = Player.Character
local Humanoid = Character.Humanoid

while wait() do
    script.Parent.Size = UDim2.new(0, (Humanoid.Health / Humanoid.MaxHealth * 225), 1, 0)
end

就像我说的,我尝试了很多东西。我尝试完全重写脚本,更改随机值等。

我期望健康栏显示在所有设备上,而不需要使用 UIAspectRatioConstraint,因为它不会根据该约束调整大小。

user-interface lua roblox luau roblox-studio
1个回答
0
投票

试试这个代码。它检测健康变化和最大健康变化。

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local HealthBar = script.Parent

local function UpdateHealthbar()
    local healthPercentage = Humanoid.Health / Humanoid.MaxHealth
    HealthBar.Size = UDim2.new(healthPercentage, 0, 1, 0)
end

UpdateHealthbar()

Humanoid:GetPropertyChangedSignal("Health"):Connect(UpdateHealthbar)
Humanoid:GetPropertyChangedSignal("MaxHealth"):Connect(UpdateHealthbar)

© www.soinside.com 2019 - 2024. All rights reserved.