Damage only works after function

I’m working on a script for a sword. The script highlights the enemy red indicating that you hit them before damaging them. The problem is that the damage only works after the highlight animation plays.

Highlight script:

local function light(glow) --Highlights the hrp
	for i = 0,math.floor(50/speed),1 do
		glow.FillTransparency -= .01*speed
		wait(0)
	end
	for i = 0,math.floor(50/speed),1 do
		glow.FillTransparency += .01*speed
		wait(0)
	end
	print("Done")
end

Thats probably because there is a loop in the way, One of the main things you have to remember about Loops is that they yield the Thread until they are broken, which would mean that they will not run any code after them until they are gone, A Simple solution would be to have them run in a separate thread, or run separately from the current code going on, which can be done by using the coroutine library or the task library.

I read both of the links you sent and I’m not sure how to do this still. Could you explain a bit?

Here’s my function also:

tool.Activated:Connect(function(attack)
	hitting = true
	tool:WaitForChild("Parts").Blade.Touched:Connect(function(hit)
		if debounce then return end
		if hit.Parent:FindFirstChild("Humanoid") then
			local humanoid = hit.Parent.Humanoid
			local glow = hit.Parent.glow
			debounce = true
			humanoid:TakeDamage(15)
			local Creator_Tag = Instance.new("ObjectValue")
			Creator_Tag.Name = "creator"
			Creator_Tag.Value = script.Parent.Parent.Parent
			Debris:AddItem(Creator_Tag, 2)
			Creator_Tag.Parent = humanoid
			light(glow)
		end
		task.wait(1) -- Cooldown
		debounce = false
	end)
	hitting = false
end)
coroutine.wrap(light)(glow)
task.spawn(light, glow)

The Documentation should show you how it works.