Only 1/2 of script is working

Out of 2 script parts (light/heavy bleeding), only heavy bleeding coroutine and UI works, incase of having health in range of light bleeding it just does nothing.

head.Changed:Connect(function()
-- not working part
	if head.Value <= 80 then
		lightbleeding = true
		print("2")
	elseif head.Value >= 81 then
		lightbleeding = false
		print("1")
	end

	if lightbleeding == true then
		lightbleeding = false
		player.PlayerGui.MainGui.Frame.Health.ImageTransparency = 0
		BloodScreen:Play()
		print("8")

		coroutine.wrap(function()
			while lightbleeding == true do
				wait(5)
				hum:TakeDamage(3)
			end
		end)
	elseif lightbleeding == false then
		player.PlayerGui.MainGui.Frame.Health.ImageTransparency = 1
		print("7")
	end
-- working part
	if head.Value <= 40 then
		heavybleeding = true
		print("6")
	elseif head.Value >= 41 then
		heavybleeding = false
		print("5")
	end
	if heavybleeding == true then
		lightbleeding = false
		player.PlayerGui.MainGui.Frame.Health.ImageTransparency = 0
		player.PlayerGui.MainGui.Frame.LowHealth.ImageTransparency = 0
		BloodScreen:Play()
		BloodScreenLowHP:Play()
		print("8")
		
		coroutine.wrap(function()
			while heavybleeding == true do
				wait(5)
				hum:TakeDamage(3)
			end
		end)
	elseif heavybleeding == false then
		player.PlayerGui.MainGui.Frame.Health.ImageTransparency = 1
		player.PlayerGui.MainGui.Frame.LowHealth.ImageTransparency = 1
		print("7")
	end
end)

Does it print the numbers in that section?

Yeah it does, however not putting the UI neither running coroutine.wrap part.

First thing first, let’s make the script readable, using the fact that you can create a variable out of those operators “<,>,<=,>=”.

head.Changed:Connect(function()
-- not working part
        local lightbleeding = head.Value <= 80 
         
	if lightbleeding then
		lightbleeding = false --this part is in a conflict with that one below.         
		player.PlayerGui.MainGui.Frame.Health.ImageTransparency = 0
		BloodScreen:Play()

		coroutine.wrap(function()
			while lightbleeding do --that one part, loop never really does take place.
				wait(5)
				hum:TakeDamage(3)
			end
		end)
	else --there's no point in elseif statement as there are only 2 states in a boolean
		player.PlayerGui.MainGui.Frame.Health.ImageTransparency = 1
		print("7")
	end
-- working part
        --you could do the same as up here
	if head.Value <= 40 then
		heavybleeding = true
		print("6")
	elseif head.Value >= 41 then
		heavybleeding = false
		print("5")
	end
	if heavybleeding == true then
		lightbleeding = false
		player.PlayerGui.MainGui.Frame.Health.ImageTransparency = 0
		player.PlayerGui.MainGui.Frame.LowHealth.ImageTransparency = 0
		BloodScreen:Play()
		BloodScreenLowHP:Play()
		print("8")
		
		coroutine.wrap(function()
			while heavybleeding == true do
				wait(5)
				hum:TakeDamage(3)
			end
		end)
	elseif heavybleeding == false then
		player.PlayerGui.MainGui.Frame.Health.ImageTransparency = 1
		player.PlayerGui.MainGui.Frame.LowHealth.ImageTransparency = 1
		print("7")
	end
end)

As you can see the problem is caused by the conflict between coroutine and a lightbleeding being set to false, stopping the loop. The solution could be that you will remove lightbleeding being set to false before the coroutine and turn it into a global…

3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.