Having some issues with my logic

I’m trying to create a simple health bar updater, but here’s the catch. The GUI it’s located in does not reset upon spawn, which would make the typical health updating script work only one time. So I had to make some changes to create new events so that it will continue to function. For whatever reason, I can’t get it to work. It works for the first time, but after that it just does not work. I can’t figure out my logic…any help appreciated, thanks!

local plr = game.Players.LocalPlayer
local character = plr.Character or plr.CharacterAdded:Wait()

local function update_text(health)
    script.Parent.TextLabel.Text = "Health [" .. math.floor(health+.5) .. "]"
    script.Parent.Bar:TweenSize(UDim2.new(health/100,0,1,0),'Out','Quad',.01,true)
 end

 local function create_event(rig)
     update_text(100)
     rig:WaitForChild("Humanoid").HealthChanged:Connect(function(health)
	     update_text(health)
	     if health <= 0 then
		     print'awaiting test'
		     local test
		      repeat
			    wait()
			    test = ((plr.Character ~= rig) and plr.Character)
		     until test
		     create_event(test)
		    print'test made yay'
	     end
    end)
 end

 create_event(character)
2 Likes

Whoops! I just realized what’s going on here. It does work when I reset my character from the menu, but it doesn’t work when I get killed by the mobs I have in the game. The reason for this is that I actually end up using :Destroy() on the character, and therefor, this event would never intercept it and would never run again.

Apologies for rushing into making this thread, I didn’t think of this at all.

4 Likes