Trying to make damage indicator expire after 6 seconds

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?
  • I am trying to make it so that this damage indicator expires after 6 seconds if no damage has been taken. Don’t worry about the fact that that there is no compatibility for multiple characters taking damage, I’ll get that sorted myself.
  1. What is the issue?
  • Instead of the indicator expiring after 6 seconds of no damage being dealt, it does not disconnect and expires 6 seconds after it is first added.
  1. What solutions have you tried so far?
  • Looked all around DevForum with tags like “damage indicator”, “disconnect”, “indicator”, etc.

  • Asked ChatGPT

  • Tried delay(6, function)

  • Tried wait(6)

  • Tried a for loop

Here is the code:

local player = game.Players.LocalPlayer
repeat wait() until player.Character ~= nil
local char = player.Character or player.CharacterAdded:Wait()

local tweenservice = game:GetService("TweenService")
local replicatedstorage = game:GetService("ReplicatedStorage")
local remote = replicatedstorage.Remotes.NumericText

currentgui = nil
lnum = 0
timerHandle = nil

function changeguinumber(person, gui, num, amount, oper)
	if oper == "Damage" then

		if timerHandle ~= nil then
			print("cd")
			timerHandle:Disconnect()
			timerHandle = nil
		end

		local att = gui.Adornee
		att.WorldCFrame = person.HumanoidRootPart.CFrame

		gui.Template.TextColor3 = Color3.fromRGB(200,0,0)

		local txt = tostring("-" .. math.round((num + amount)*10)/10)
		print(txt)
		local subbedtxt = string.sub(txt, 2, #txt)

		gui.Template.Text = txt
		gui.Template.Shadow.Text = txt
		lnum = tonumber(subbedtxt)
		print(lnum)

		print("dmg")
		gui.StudsOffsetWorldSpace -= Vector3.new(0,1,0)
		tweenservice:Create(gui, TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.Out, 0, false, 0),
			{StudsOffsetWorldSpace = gui.StudsOffsetWorldSpace + Vector3.new(0,1,0)}):Play()

		timerHandle = coroutine.wrap(function()
			wait(6)
			print("destroy")
			currentgui:Destroy()
			currentgui = nil
			lnum = 0
			timerHandle = nil
		end)()

	elseif oper == "Heal" then

		if timerHandle then
			timerHandle:Disconnect()
			timerHandle = nil
		end

		local att = gui.Adornee
		att.WorldCFrame = person.HumanoidRootPart.CFrame

		gui.Template.TextColor3 = Color3.fromRGB(53, 204, 63)

		local txt = tostring("+" .. math.round((amount)*10)/10)
		local subbedtxt = string.sub(txt, 2, #txt)

		gui.Template.Text = txt
		gui.Template.Shadow.Text = txt

		lnum = tonumber(subbedtxt)
		print("destroy")
		gui.StudsOffsetWorldSpace -= Vector3.new(0,1,0)
		tweenservice:Create(gui, TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.Out, 0, false, 0),
			{StudsOffsetWorldSpace = gui.StudsOffsetWorldSpace + Vector3.new(0,1,0)}):Play()

		timerHandle = coroutine.wrap(function()
			wait(6)
			print("uhh")
			currentgui:Destroy()
			currentgui = nil
			lnum = 0
			timerHandle = nil
		end)()
	end
end

function makegui(damage, person, oper)
	local att = Instance.new("Attachment")
	att.Parent = workspace.BaseAttPart
	att.WorldCFrame = person.HumanoidRootPart.CFrame

	local indicator = script.Indicator:Clone()
	indicator.Enabled = true
	indicator.Parent = person
	indicator.Adornee = att
	indicator.Template.Text = damage
	indicator.Template.Shadow.Text = damage

	return indicator
end

function showdamage(damage, person, oper)

	local gui

	if oper == "Damage" then

		if not currentgui then
			local newGui = makegui(damage, person, oper)
			currentgui = newGui
			changeguinumber(person, newGui, lnum, damage, oper)
			
		elseif currentgui then
			gui = currentgui
			changeguinumber(person, gui, lnum, damage, oper)
		end

	elseif oper == "Heal" then

		if not currentgui then
			local newGui = makegui(damage, person, oper)
			currentgui = newGui
			changeguinumber(person, newGui, lnum, damage, oper)
			
		elseif currentgui then
			gui = currentgui
			changeguinumber(person, gui, lnum, damage, oper)
		end
	end
end

remote.OnClientEvent:Connect(function(person, damage, oper)
	if person ~= char then
		showdamage(damage, person, oper)
	end
end)

Thanks for any help you can provide. :happy1:

Guess I didn’t look all around DevForum, and found this:

3 Likes

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