Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can i disconnect the event when the animation as stopped playing?

The script is:

game.ReplicatedStorage.Event.SwordAttacked.OnServerEvent:connect(function(Player, Equip)
	print("Trying to punch but there is nothing wrong")
local Char = Player.Character	
local Swords = game.ServerStorage:FindFirstChild(Player.Name):WaitForChild("SwordSlot").Value
local animTrackAttack = Char.Humanoid:LoadAnimation(Player.Backpack:FindFirstChild(Swords.."Equip").Precise_Config.SlashAnim)
animTrackAttack:Play()
for i,v in pairs(Char:FindFirstChild("AttackSword"):GetChildren()) do
	if v.Name == "Blade" then
		v.Touched:connect(function(hit)
			if hit.Parent:findFirstChild("Life") then
            hit.Parent:findFirstChild("Life"):TakeDamage(tonumber(game.ReplicatedStorage.ItemDrop:FindFirstChild(game.ServerStorage:FindFirstChild(Player.Name):WaitForChild("SwordSlot").Value).Damage.Value))
           end
		end)
	end
end
wait(animTrackAttack.Length)
for i,v in pairs(Char:FindFirstChild("AttackSword"):GetChildren()) do
	if v.Name == "Blade" then
		v.Touched:Disconnect()
	end
end
end)

the error is 01:01:31.560 - Disconnect is not a valid member of RBXScriptSignal 01:01:31.560 - Stack Begin 01:01:31.561 - Script 'Workspace.FuncEvent.SwordFunction', Line 19 01:01:31.561 - Stack End

1 answer

✓ Accepted answer

It's disconnect, not Disconnect (notice the capitalization difference).

However, you cannot just tell an event to disconnect anything listening to it (which is sort of what you're trying to do). Instead, you must keep track of all the connections (which are returned by connect) in a table and then disconnect those.

--after line 6
local connections = {}
--part of line 9
connections[#connections + 1] = --rest of line 9 here
--instead of lines 17-21
for i = 1, #connections do
	connections[i]:disconnect()
end

Note that the beginning of line 9, connections[#connections + 1] = ..., is the same idea as using table.insert(connections, ...) (replacing '...' with the code, of course).

This discussion is preserved as it was. New answers, comments, and votes are closed.