Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Doesn't remove the label when health changes?

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

1 comment

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.

3 answers

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.

'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

3 comments

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

0

I dont see any waiting value higher than 180 on code you showed, when the function Track() fires is still in myth

0

Lmao he unknowingly used recursion in his script LOl

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

This discussion is preserved as it was. New answers, comments, and votes are closed.