Issue with AnimationEvents

Hey, QueuedUsername, here. Recently, I’ve just learned of AnimationEvents and the GetMarkedReachedSignal() function. I’ve created a reloading function for my gun involving a ejection function I’ve made. But every time I reload the weapon, the part that is ejected is added by another copy.I don’t understand what’s going on, I’m simply looking for an error or issue I’ve made in my code.


elseif currentFiringMode == 'BOLT_ACTION' then -- Ignore this statement
		if moduleRequired.MAG_CAPACITY ~= moduleRequired.MAG_CONSTANT then
			while moduleRequired.MAG_CAPACITY ~= moduleRequired.MAG_CONSTANT and moduleRequired.RSRV_CAPACITY > 0 do
				moduleRequired.MAG_CAPACITY = moduleRequired.MAG_CAPACITY + 1
				moduleRequired.RSRV_CAPACITY = moduleRequired.RSRV_CAPACITY - 1
				gui.Frame.Counter.Text = '...'
			end
		end
		if reloadT then reloadT:Play() reloadT:GetMarkerReachedSignal("EjectPart"):Connect(function() EjectCasing(tool.StripPiece, tool.EjectionPart2, 1) end) end
	end

Reload Function

function EjectCasing(partToClone, partToEjectFrom, noOfCasings)
	for i = 1, noOfCasings do
		local shell = partToClone:Clone()
		shell.Transparency = 0 
		shell.Name = 'Casing_Clone'
		shell.Parent = workspace
		shell.CFrame = partToEjectFrom.CFrame
		shell.CanCollide = true
		shell.Velocity = partToEjectFrom.CFrame.LookVector * moduleRequired.VELOCITY
		shell.RotVelocity = partToEjectFrom.CFrame.LookVector * moduleRequired.ROT_VELOCITY
		game:GetService('Debris'):AddItem(shell, .8)
	end
end

Part Ejection Function


Animation


End Result

If you need extra screenshots, send me a comment below! Thanks for reading! :cowboy_hat_face:

Please don’t screen shot for code. Use the three back ticks to post the code in that screen shot for part ejection is way too hard to read first glance.

--like so

Apologies, I’ll edit it now, thanks for letting me know.

Thanks :+1:

Anyways, this seems like a multiple connection problem which is why multiple casings are ejected as you make a new connection every time you want to play the animation hence +1 more casing I believe.

I recommend spacing out the if statements according to the style guide in order to see the problem much easier, plus tab indenting

Perhaps disconnecting the event is the easiest way to solve as is right after the connection was made.

if reloadT then
reloadT:Play() 
local ejectCasingConnection = reloadT:GetMarkerReachedSignal("EjectPart"):Connect(function() 
EjectCasing(tool.StripPiece, tool.EjectionPart2, 1) 
end) 

ejectCasingConnection:Disconnect()

end

However, I think you should just initialize the connection outside the script like so:

--somewhere at the top of the script to initialize the eject casing connection
local ejectCasingConnection = reloadT:GetMarkerReachedSignal("EjectPart"):Connect(function() 
EjectCasing(tool.StripPiece, tool.EjectionPart2, 1) 
end) 


--then
if reloadT then
reloadT:Play() 
--the connection will automatically play once the animation is finished
end

1 Like

Oooh, you know what. I was skimming through an article and I noticed the :Disconnect() function but didn’t know what to do, thank you!