低NumberValue对该脚本不执行任何操作

时间:2018-12-26 18:56:32

标签: lua scripting roblox

我不再依赖类人动物的健康,而是决定使用自定义健康。新的生命值依价值而定,并且运作良好,我想使角色结冰几秒钟,然后将其传送到特定的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

该脚本通常无法正常工作。即使启用并放置在正确的位置,它也永远不会起作用。

2 个答案:

答案 0 :(得分:0)

如果我理解正确的话,您希望将角色的健康状况在冻结几秒钟后传送到低于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)

答案 1 :(得分:0)

以下是一些修复程序,如果这是服务器脚本,则更改为:

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

希望它能起作用,别忘了选择它作为正确答案,请喜欢它= D