Task.delay warning just appeared

Hello, so i have this code and it just worked fine, but now, it is just giving me much task.delay warning, take a look, also it gives the warning at line 30.

--
	local HealthBar = script.Parent.HealthBar
	local PlayerService = game:GetService("Players")
--
	local TweenService = game:GetService("TweenService")
	local TweenInformation = TweenInfo.new(0.5, Enum.EasingStyle.Sine, Enum.EasingDirection.Out,0,false,0)
	local CharacterHealthLabel = script.Parent.CharacterHealth
--
	local LastHealth = 0
--
	while task.wait() do
		if PlayerService.LocalPlayer.Character then
			
			local Humanoid = PlayerService.LocalPlayer.Character:WaitForChild("Humanoid")
			
			local CurrentHealth = Humanoid.Health
			local NewFrameSize = UDim2.new(CurrentHealth / 100,0,1,0)
			local Tween = TweenService:Create(HealthBar, TweenInformation, {Size = NewFrameSize})
			
			Tween:Play()
			
			if CurrentHealth > LastHealth then
				for I = LastHealth, CurrentHealth, 1 do
					script.Parent.CharacterHealth.Text = (tostring(math.floor(I)).." %")
					task.wait(0.256/CurrentHealth - LastHealth)
				end
			else
				for I = LastHealth, CurrentHealth, -1 do
					script.Parent.CharacterHealth.Text = (tostring(math.floor(I)).." %")
					task.wait(0.256/(CurrentHealth - LastHealth)) --Here is line 30.
				end
			end
			
			LastHealth = CurrentHealth
		end
	end
--
1 Like

What exactly is the error message that you are getting? So far it looks fine to me.

1 Like

task.wait(0.256/(CurrentHealth - LastHealth))
This isnt about the fact that your subtracting 100 by 0, its about the fact how task.wait() would handle your calculation.

    for I = LastHealth, CurrentHealth, 1 do
        script.Parent.CharacterHealth.Text = tostring(math.floor(I)) .. " %"
        task.wait(math.abs(0.256 / math.max(CurrentHealth - LastHealth, 1)))
    end
else
    for I = LastHealth, CurrentHealth, -1 do
        script.Parent.CharacterHealth.Text = tostring(math.floor(I)) .. " %"
        task.wait(math.abs(0.256 / math.max(LastHealth - CurrentHealth, 1)))
    end
end

This would be a better approach to this script as math.max guarantees you no division by zeros, which would result in an error if divided; and math.abs guarantees you that no negative value would be passed to task.wait(). CurrentHealth - LastHealth would be positive in this script, which would result in a positive wait time, same with LastHealth - CurrentHealth.

1 Like

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