Need help with Trails

I have created trails and made it work in my combat script so that it emits during the animations.

As you can see I have problems disabling the trail as I don’t know when to place the remove/disable part of the trail.
I have included a snippet of my code if anyone can help me.

    local ran = math.random(1,2)
    if ran == 1 then	
		Animations['LeftPunch']:Play()
        Sounds['Miss']:Play()
		LHTrail.Enabled = true
		LHTrail.Attachment0 = LeftHand.LeftBotCorner
		LHTrail.Attachment1 = LeftHand.LeftTopCorner
		LHTrail.Parent = LeftHand
		LHTrail.Color = ColorSequence.new(LHColor)
		t = true
		Character.LeftHand.Touched:connect(function(ouch)
			if not t then return end
			local e = ouch.Parent:FindFirstChild("Humanoid")
			if e and e ~= Character.Humanoid then
				t = false
				Remotes.HitEffect:FireServer(Hit, HitRing, ouch)
				Sounds['Hit']:Play()
				damage(ouch)
			end
		end)
    end

So the way I got around this was to name the keyframes something to denote when an action should start, and when it should be ended. If you go into the animation plug in, import your anim and then right click a frame and rename it you can then use code like this:

Animation.KeyframeReached:connect(function (key)
	if key == "StartFrame" then --> Name of your start keyframe
		-- Instance your trail here
	elseif key == "EndFrame" then
		-- Remove your trail here
	end
end)

Edit: I should note that this has been superseded by the method :GetMarkerReachedSignal(), so you would be better off creating a marker keyframe instead, the following code can then be used:

Animation:GetMarkerReachedSignal("NameOfYourKeyFrame"):connect(function ()
	-- stuff
end)
2 Likes

Thank you! This helped me fix it.

1 Like