Low NumberValue 对脚本没有任何作用

Low NumberValue does nothing for the script

我决定不依赖于人形生物的生命值,而是使用自定义生命值。新的生命值按价值计算并且效果很好,我想让它在角色冻结几秒钟的地方,然后将它们传送到特定的 Vector3 值。

我尝试用不同的方式编写脚本,但所有的脚本都不起作用。我什至试图把它放到玩家位置不同的地方,但也失败了。

--Responsible for healing a player's humanoid's health

-- declarations
local Figure = script.Parent
local Head = Figure:WaitForChild("Head")
local Humanoid = Figure:WaitForChild("Humanoid")
local PlayerHealth = game.Players.LocalPlayer.Character.Data.Health
local Player = game.Players.LocalPlayer.Character.Humanoid



if PlayerHealth.Value < 30 then
    Player.WalkSpeed = 0
    wait(5)
    Player.WalkSpeed = 16
end

该脚本通常无法正常工作。即使它被启用并放在正确的位置,它也从未起作用。

如果我没理解错的话,你想在角色被冻结几秒钟后,当它的生命值低于 30 时传送到一个位置。然后,您应该在每次值更改时检查 PlayerHealth 值,方法是将它连接到一个函数以捕捉它的健康值低于 30 的时刻:

local Figure = script.Parent
local Head = Figure:WaitForChild("Head")
local Humanoid = Figure:WaitForChild("Humanoid")
local Data = Figure:WaitForChild("Data") --In any case if the data loads after the script runs
local PlayerHealth = game.Players.LocalPlayer.Character.Data.Health
local Player = game.Players.LocalPlayer.Character.Humanoid


PlayerHealth.Changed:connect(function()--Here you check the value every time it changes.
if PlayerHealth.Value < 30 then
    Player.WalkSpeed = 0
    wait(5)
    -- you can add teleportation here.
    --Figure:MoveTo(Position)
    Player.WalkSpeed = 16
end
end)

这里有一些修复,如果这是服务器脚本,则更改为:

local Figure = script.Parent
local Head = Figure:WaitForChild("Head")
local Humanoid = Figure:WaitForChild("Humanoid")
local Player = game.Players:GetPlayerFromCharacter(Figure) --It will get the player from his character as server scripts can't access LocalPlayer
local Health = Player:WaitForChild("Data"):WaitForChild("Health")


Health.Changed:Connect(function()
    if Health.Value < 30 then
        Player.WalkSpeed = 0
        wait(5)
        -- Add more code here
        Player.WalkSpeed = 16
    end
end)

否则,如果它是本地脚本,则只需更改此

local Player = game.Players:GetPlayerFromCharacter(Figure)

local Player = game.Players.LocalPlayer

希望它有用,不要忘记select它作为正确答案,请喜欢它=D