Help disconnecting functions

i have multiple connections to a table for animations.
when the disconnect function runs, the animation will still play at the end (1)

in other words, when i unequip the animation to return to the equipped animation still runs


local LoadedAnimations = {}
local connections = {}
local Animations = {
	script:WaitForChild("Hold"), 
	script:WaitForChild("Idle"),
	script:WaitForChild("Click"),
	script:WaitForChild("Reload"),
	script:WaitForChild("Aim")
}

tool.Equipped:Connect(function()

local char = player.Character
	local hum = char:WaitForChild("Humanoid")
	
	local Aim = false
	local idle = false
	local Reload = false
	local Unequipped = true
	
	-- Animations to humanoid
	LoadedAnimations[1] = hum:LoadAnimation(Animations[1])
	LoadedAnimations[2] = hum:LoadAnimation(Animations[2])
	LoadedAnimations[3] = hum:LoadAnimation(Animations[3])
	LoadedAnimations[4] = hum:LoadAnimation(Animations[4])
	LoadedAnimations[5] = hum:LoadAnimation(Animations[5])
	-- play defualt
	LoadedAnimations[1]:Play()
	
	--Died
	connections[1] = hum.Died:Connect(function()
		DisconnectAll()
	end)

    connections[5] = UserInputService.InputBegan:Connect(function(Input,GPE)
		if GPE then return end
		if Input.KeyCode == Enum.KeyCode.R then
			if Reload == false then
				Reload = true
				
				StopOtherAnimations(4)
				LoadedAnimations[4]:Play()
				
				wait(tonumber(Configuration.ReloadDelay.Value/2))
				
				particleemiter.Enabled = true
				
				wait(tonumber(Configuration.ReloadDelay.Value/2))
				
				particleemiter.Enabled = false
				
				LoadedAnimations[4]:Stop()
				LoadedAnimations[1]:Play()
				
				Reload = false
			end
		end
	end)
end)

tool.Unequipped:Connect(function()
	particleemiter.Enabled = false
	StopOtherAnimations(0)
	DestroyFolderItems(LoadedAnimations)
	DisconnectAll()
end)

video of what happening
robloxapp-20221216-1208103.wmv (1.1 MB)
(accidently clicked enter before typing everything on devfourm, sry)

Try checking if the animation is still running when the tool is unequipped. If it is still running, keep trying to stop the animation.

1 Like

i can stop the animation [4]. it is that the script will still run

LoadedAnimations[1]:Play()

(the one near the end)
even after the function is disconnected

If the script will still run when it’s disconnected, try disabling it when it’s unequipped then using another script to enable it when it’s equipped. I have only been scripting for a few month’s, so I don’t know if this will work.

1 Like

all good, I have also just started scripting. Although the problem that might occur doing this is that if the animation will still play, it might mean that the animation will still play and stay there without being deleted/removed/stopped. meaning spamming the tool can create a bunch of animation overlapping each other on one character, lagging the server. ( i might be very wrong tho)

Stop all animations before disabling the script. Then try it. Might work. SORRY ABOUT THAT, DON’T DISABLE SCRIPT BEFORE STOPPING ANIMATIONS

1 Like

I fixed it by accident.
I found out that when uneqipping it goes into backpack so i can check that

if script.Parent.Parent:IsA("Backpack")then
					
else
	LoadedAnimations[4]:Stop()
	LoadedAnimations[1]:Play()
end

no problem about last post

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