Ad
Log in to vote
1

Doesn't remove the label when health changes?

Asked by
TechModel 118
11 months ago

I'm trying to make it so on line 9 the "HealthChanged" makes it return and keeps the duration of the text for 3 everytime i hit the humanoid, but it doesn't instead of that, it says visible forever.

function Track(Humanoid)
    local Last = Humanoid.Health
    local function HealthChanged(Left)
        if Left < Last then
            local Part = Humanoid.Parent:FindFirstChildWhichIsA("BasePart")
            if Part then
                -- label stuff i put here --
                wait(.1)
                if HealthChanged then
                    return

                else
                wait(3)
                    label.Visible = false
                    end
            end
            Last = Left
        end
    end
    Humanoid.HealthChanged:connect(HealthChanged)
end
0
Also when other player do damage, i see their damage, strange when its a local script and i want the player to see it only them and their humanoid damage, not others. TechModel 118 — 11mo

3 answers

Log in to vote
1
Answered by 11 months ago
Edited 11 months ago

Unknowingly, you used recursion which silenced the error. This is a very awkward script just reading it, I will attempt to help you however.

Your script (For reference):

function Track(Humanoid)
    local Last = Humanoid.Health
    local function HealthChanged(Left)
        if Left < Last then
            local Part = Humanoid.Parent:FindFirstChildWhichIsA("BasePart")
            if Part then
                -- label stuff i put here --
                wait(.1)
                if HealthChanged then
                    return

                else
                wait(3)
                    label.Visible = false
                    end
            end
            Last = Left
        end
    end
    Humanoid.HealthChanged:connect(HealthChanged)
end

My edited script:

function Track(Humanoid)
    local Last = Humanoid.Health
    Humanoid.HealthChanged:Connect(function(health)
        if health < Last then
            Last = health
            local Part = Humanoid.Parent:FindFirstChildWhichIsA("BasePart")
            if Part then
                local label = script.Parent -- whatever the label was
                label.Visible = false
            end
        end
    end)
end
-- make sure to Disconnect() HealthChanged to prevent memory leaks

Note: One concern is that I'm not sure why you added "if HealthChanged", I'm just assuming you tried to detect when the health changed and forgot it was already being monitored.

Also, back early to what I said about recursion, if that actually ran which it didn't because no parameter was inputted, it would have reached the maximum frames in no time and you would have crashed.

Also make sure you call Track(humanoid) somewhere in the script so it actually begins monitoring the health changes.

Ad
Log in to vote
0
Answered by 11 months ago
Edited 11 months ago

'HealthChanged' is function in your code, not true or false so its weird to do

if HealthChanged then
    return

and I think you better set value of "Last" more frequently

I dont know when function Track works but If it doesnt set frequently enough the phenomenon - when you get your health pretty low then regen health- Value of Last is low and your current health is high, so it can't be done after

if Left < Last then
0
But then the problem is the text still shows more than 3 seconds? Thats why it was dumb of me to try out using the "HealthChanged" function TechModel 118 — 11mo
0
I dont see any waiting value higher than 180 on code you showed, when the function Track() fires is still in myth TerranRecon 49 — 11mo
0
Lmao he unknowingly used recursion in his script LOl greatneil80 2612 — 11mo
Log in to vote
0
Answered by 11 months ago

Hello Chap

I have seen you need some Help, I will give you a fixed script [Maybe]

And also errors you did

First of all: I didn't see a label variable

And second: connect keyword must be like this : "Connect"

Here is your fixed script



function Track(Humanoid) local Last = Humanoid.Health local function HealthChanged(Left) if Left < Last then local Part = Humanoid.Parent:FindFirstChildWhichIsA("BasePart") if Part then -- label stuff i put here -- local label = .parent -- Set here your label wait(.1) if HealthChanged then return else wait(3) label.Visible = false end end Last = Left end end Humanoid.HealthChanged:Connect(HealthChanged) -- Capital C in "Connect" end

I hope it works for you :D

Answer this question