Script keeps animations from working

Hiya, so I am currently working on my own project. Anyways, so I have a script which makes it so the sword equipped shows up on your back. It works, but it stops the animations from working. But, if the script isn’t in the sword, the animations starts working again.
If needed, then here is the script for the sword on your back and the script for the animations.
https://gyazo.com/6de9025442134c67c5fc182b5cee32c4
It would be helpful if someone could help.

-- SwordOnBackScript
while wait() do
    if script.Parent.Parent.Name == "Backpack" then
        local Chararacter = script.Parent.Parent.Parent.Character

        if Chararacter then
              local UpperTorso = Chararacter:FindFirstChild("Torso")
              local Weapon = Chararacter:FindFirstChild(script.Parent.Name)

              if UpperTorso and not Weapon then
                   local WeaponOnLeg = Instance.new("Model")
                   WeaponOnLeg.Name = script.Parent.Name
                   WeaponOnLeg.Parent = Chararacter

                   Handle = script.Parent.Handle:Clone()
                Handle.Name = "Handle"
                Handle.Parent = WeaponOnLeg

                local LegWeld = Instance.new("Weld")
                LegWeld.Name = "WeldOnLeg"
                LegWeld.Part0 = UpperTorso
                LegWeld.Part1 = Handle
                LegWeld.C0 = CFrame.new(0,-0.2,0.5)
                LegWeld.C0 = LegWeld.C0 * CFrame.fromEulerAnglesXYZ(math.rad(0),math.rad(-180),math.rad(-137.12))
                LegWeld.Parent = Handle
              end
        end

    else
        if Handle.Parent then 
              Handle.Parent:Destroy()
         end
    end
end
-- Animations Script
tool = script.Parent
local debounce = false

tool.Equipped:Connect(function()
	local equip = script.Parent.Parent.Humanoid:LoadAnimation(script.Parent.equip)
	local idle = script.Parent.Parent.Humanoid:LoadAnimation(script.Parent.idle)
	equip:play()
	wait(1.5)
	idle:play()
	tool.Unequipped:Connect(function()
		equip:stop()
		idle:stop()
	end)
end)

tool.Activated:Connect(function()
	if not debounce then
	debounce = true
	local slash = script.Parent.Parent.Humanoid:LoadAnimation(script.Parent.slash)
	slash:play()
	wait(1)
	debounce = false
	end
end)
1 Like

I seem to understand everything but this part:

So the equipped sword is suppose to stay/show up on your back? Also, is the while loop checking to see if the player has the tool equipped so that it places the weapon on the player’s back?

My bad, I meant that the sword shows up on your back when unequippied.

Gotcha, so I’ll try my best to help you out here since there are a couple of problems that lead to another.

Did you by chance have your while loop before your animation functions? If that is the case, that is why your tool is not being animated; the functions are never reached because your script is still cycling through the while loop.
Taking this one step at a time so that you understand this code. Or if this is TL:DR, the code is at the bottom.

The Animation Script


I noticed that you used a wait between your equip and idle animations. There is a built-in function for animations, and instead of using a wait, which might cut your animation off while it’s playing, you could use:

equip.Stopped:Connect(function() --- This fires the code when the animation is done.
    idle:Play()
end)

and

slash.Stopped:Connect(function()
   debounce = false
end)

This is much more clean, and does not make you have to predict or remember how long your animation is for putting in a wait number.

Sword Script


Also, the while loop is unnecessary since the sword’s location is only changed when you equipped the tool, so instead of the while loop constantly checking for the weapon, you can just make a function that calls for putting the weapon on the back when you unequip your weapon like this:

function WeaponLocation()
    if script.Parent.Parent.Name == "Backpack" then
        local Chararacter = script.Parent.Parent.Parent.Character

        if Chararacter then
              local UpperTorso = Chararacter:FindFirstChild("Torso")
              local Weapon = Chararacter:FindFirstChild(script.Parent.Name)

              if UpperTorso and not Weapon then
                   local WeaponOnLeg = Instance.new("Model")
                   WeaponOnLeg.Name = script.Parent.Name
                   WeaponOnLeg.Parent = Chararacter

                   Handle = script.Parent.Handle:Clone()
                   Handle.Name = "Handle"
                   Handle.Parent = WeaponOnLeg

                   local LegWeld = Instance.new("Weld")
                   LegWeld.Name = "WeldOnLeg"
                   LegWeld.Part0 = UpperTorso
                   LegWeld.Part1 = Handle
                   LegWeld.C0 = CFrame.new(0,-0.2,0.5)
                   LegWeld.C0 = LegWeld.C0 * CFrame.fromEulerAnglesXYZ(math.rad(0),math.rad(-180),math.rad(-137.12))
                   LegWeld.Parent = Handle
              end
        end

    else
        if Handle.Parent then 
              Handle.Parent:Destroy()
         end
    end
end

so when it’s time to equip/unequip the weapon, your code would look like this:

tool.Equipped:Connect(function()
	local equip = script.Parent.Parent.Humanoid:LoadAnimation(script.Parent.equip)
	local idle = script.Parent.Parent.Humanoid:LoadAnimation(script.Parent.idle)
	equip:play()
        equip.Stopped:Connect(function() --- This fires the code when the animation is done.
            idle:Play()
        end)
	tool.Unequipped:Connect(function()
	    equip:stop()
	    idle:stop()
           WeaponLocation()
	end)
        WeaponLocation() --- Here is where you look to see where the weapon is.
end)

tool.Activated:Connect(function()
	if not debounce then
	debounce = true
	local slash = script.Parent.Parent.Humanoid:LoadAnimation(script.Parent.slash)
	slash:play()
	wait(1)
	debounce = false
	end
        WeaponLocation --- Here it is again.
end)

So by time all of that is done, your code (which is in one script now) should look like this:

function WeaponLocation()
    if script.Parent.Parent.Name == "Backpack" then
        local Chararacter = script.Parent.Parent.Parent.Character

        if Chararacter then
              local UpperTorso = Chararacter:FindFirstChild("Torso")
              local Weapon = Chararacter:FindFirstChild(script.Parent.Name)

              if UpperTorso and not Weapon then
                   local WeaponOnLeg = Instance.new("Model")
                   WeaponOnLeg.Name = script.Parent.Name
                   WeaponOnLeg.Parent = Chararacter

                   Handle = script.Parent.Handle:Clone()
                   Handle.Name = "Handle"
                   Handle.Parent = WeaponOnLeg

                   local LegWeld = Instance.new("Weld")
                   LegWeld.Name = "WeldOnLeg"
                   LegWeld.Part0 = UpperTorso
                   LegWeld.Part1 = Handle
                   LegWeld.C0 = CFrame.new(0,-0.2,0.5)
                   LegWeld.C0 = LegWeld.C0 * CFrame.fromEulerAnglesXYZ(math.rad(0),math.rad(-180),math.rad(-137.12))
                   LegWeld.Parent = Handle
              end
        end
    else
        if Handle.Parent then 
              Handle.Parent:Destroy()
         end
    end
end


tool.Equipped:Connect(function()
	local equip = script.Parent.Parent.Humanoid:LoadAnimation(script.Parent.equip)
	local idle = script.Parent.Parent.Humanoid:LoadAnimation(script.Parent.idle)
	equip:play()

        equip.Stopped:Connect(function() --- This fires the code when the animation is done.
            idle:Play()
        end)

	tool.Unequipped:Connect(function()
	    equip:stop()
	    idle:stop()
            WeaponLocation()  --- Here is where you look to see where the weapon is.
	end)

        WeaponLocation() --- Here as well.
end)

tool.Activated:Connect(function()
	if not debounce then
	    debounce = true
	    local slash = script.Parent.Parent.Humanoid:LoadAnimation(script.Parent.slash)
	    slash:play()
            slash.Stopped:Connect(function()
                debounce = false
            end)
	end
        WeaponLocation() --- Here it is again.
end)

Hopefully I did everything correctly, but that should help fix your issue.

Alright, I’ll test that out later since I don’t have access to my laptop right now and my sister is using hers. I’ll let ya know if your explaintion helps me out!

1 Like

I just tried it, and sadly… It doesn’t work. In fact, the sword doesn’t show up on my back when unequipped anymore.

Still trying to figure it out; which still isn’t working sadly. It’d be great if someone else could help. And BruiseIgnio, thanks for trying to help! @BruiseIgnio

No problem.

So I ended up testing it myself, and I noticed that the unequipped function was in equipped function; which shouldn’t happen unless you want it to fire multiple times everytime to were to activate a function… I fixed it like this:

function WeaponLocation()
    if script.Parent.Parent.Name == "Backpack" then
        local Chararacter = script.Parent.Parent.Parent.Character

        if Chararacter then
              local UpperTorso = Chararacter:FindFirstChild("Torso")
              local Weapon = Chararacter:FindFirstChild(script.Parent.Name)

              if UpperTorso and not Weapon then
                   local WeaponOnLeg = Instance.new("Model")
                   WeaponOnLeg.Name = script.Parent.Name
                   WeaponOnLeg.Parent = Chararacter

                   Handle = script.Parent.Handle:Clone()
                   Handle.Name = "Handle"
                   Handle.Parent = WeaponOnLeg

                   local LegWeld = Instance.new("Weld")
                   LegWeld.Name = "WeldOnLeg"
                   LegWeld.Part0 = UpperTorso
                   LegWeld.Part1 = Handle
                   LegWeld.C0 = CFrame.new(0,-0.2,0.5)
                   LegWeld.C0 = LegWeld.C0 * CFrame.fromEulerAnglesXYZ(math.rad(0),math.rad(-180),math.rad(-137.12))
                   LegWeld.Parent = Handle
              end
        end
    else
        if Handle.Parent then 
              Handle.Parent:Destroy()
         end
    end
end


tool.Equipped:Connect(function()
     local equip = script.Parent.Parent.Humanoid:LoadAnimation(script.Parent.equip)
     local idle = script.Parent.Parent.Humanoid:LoadAnimation(script.Parent.idle)
	equip:play()

        equip.Stopped:Connect(function() --- This fires the code when the animation is done.
            idle:Play()
        end)

        WeaponLocation() --- Here as well.
end)

tool.Unequipped:Connect(function()
    WeaponLocation()  --- Here is where you look to see where the weapon is.
end)

tool.Activated:Connect(function()
	if not debounce then
	    debounce = true
	    local slash = script.Parent.Parent.Humanoid:LoadAnimation(script.Parent.slash)
	    slash:play()
            slash.Stopped:Connect(function()
                debounce = false
            end)
	end
        WeaponLocation() --- Here it is again.
end)

and it seemed to work for me. Are there any errors popping up on your screen?

I’ll try right now. I’ll reply again quickly to let you know if it works or not.

Hmm… Looks like it isn’t working for me. A error popped up saying “Players.Cyanthian.Backpack.Firebrand.SwordOnBackScript:35: attempt to index nil with 'Equipped'”, and the sword still doesn’t show up on my back when unequipped.

1 Like

That means that the term ‘tool’ is not referencing the tool correctly.

Do you have anything like this:

local tool = script.Parent

Anywhere in your script?

1 Like

Nope, I don’t. I just rechecked the script and it doesn’t have that.

Edit: I added

local tool = script.Parent

to the script, and now it shows the sword on my back when unequipped. Though… One problem. It doesn’t show the sword on my back until I equip it and unequip it, and I’m also getting this error. “Players.Cyanthian.Backpack.Firebrand.SwordOnBackScript:28: attempt to index nil with 'Parent'
Sometimes scripting can be a real pain. :woman_facepalming:

Now looking at the code again, is Handle initiated anywhere in your code?

Yeah, it is. I’m using your script right now. I’ll send it again.

function WeaponLocation()
    if script.Parent.Parent.Name == "Backpack" then
        local Chararacter = script.Parent.Parent.Parent.Character

        if Chararacter then
              local UpperTorso = Chararacter:FindFirstChild("Torso")
              local Weapon = Chararacter:FindFirstChild(script.Parent.Name)

              if UpperTorso and not Weapon then
                   local WeaponOnLeg = Instance.new("Model")
                   WeaponOnLeg.Name = script.Parent.Name
                   WeaponOnLeg.Parent = Chararacter

                   Handle = script.Parent.Handle:Clone()
                   Handle.Name = "Handle"
                   Handle.Parent = WeaponOnLeg

                   local LegWeld = Instance.new("Weld")
                   LegWeld.Name = "WeldOnLeg"
                   LegWeld.Part0 = UpperTorso
                   LegWeld.Part1 = Handle
                   LegWeld.C0 = CFrame.new(0,-0.2,0.5)
                   LegWeld.C0 = LegWeld.C0 * CFrame.fromEulerAnglesXYZ(math.rad(0),math.rad(-180),math.rad(-137.12))
                   LegWeld.Parent = Handle
              end
        end
    else
        if Handle.Parent then 
              Handle.Parent:Destroy()
         end
    end
end

local tool = script.Parent
tool.Equipped:Connect(function()
     local equip = script.Parent.Parent.Humanoid:LoadAnimation(script.Parent.equip)
     local idle = script.Parent.Parent.Humanoid:LoadAnimation(script.Parent.idle)
	equip:play()

        equip.Stopped:Connect(function() --- This fires the code when the animation is done.
            idle:Play()
        end)

        WeaponLocation() --- Here as well.
end)

tool.Unequipped:Connect(function()
    WeaponLocation()  --- Here is where you look to see where the weapon is.
end)

tool.Activated:Connect(function()
	if not debounce then
	    debounce = true
	    local slash = script.Parent.Parent.Humanoid:LoadAnimation(script.Parent.slash)
	    slash:play()
            slash.Stopped:Connect(function()
                debounce = false
            end)
	end
        WeaponLocation() --- Here it is again.
end)

Edit: I forgot to say that I’m also getting another error saying that the animations aren’t loading which has never happened before. It’s basically this, Animation "https://assetdelivery.roblox.com/v1/asset?id=2681478414&serverplaceid=0" failed to load in "Animation.AnimationId": Animation failed to load

Ok, the issue here is that the variable Handle is not able to be referenced since it is outside the scope of the function. A little reading here will help you understand a bit. After reading that, you should reference handle and tool at the top of your script, so that before your WeaponLocation function is called, the top should look like this:

local tool = script.Parent
local handle = script.Parent.Handle --- Camel cased this, don't forget to change the other `Handle`s to `handle` to avoid error.

function WeaponLocation()
--- etc..
end

The animations error is new to me, but try this first, and we’ll go on from there. The only real issue I can think of is if the animation you are using is not owned by you.

The animations that I’m using for the swords are owned by me. Also, I’m getting this:
Something unexpectedly tried to set the parent of Firebrand to NULL while trying to set the parent of Firebrand. Current parent is Cyanthian.

Edit: The sword isn’t now showing when I equip it.

1 Like

Since tools automatically set their own parent, I do believe you don’t need this line anymore. This actually destroys the tool itself, so that the tool and handle are no longer available. Try taking this portion out.

Alright now it shows the sword. I noticed that when I equip the sword for a 3rd time, the sword is still on my back. This is what I mean:
image

Edit: I figured out why the errors for the animations weren’t working. Turns out that the animations IDs somehow got changed to different animations which I didn’t own, which is odd. So I copied the IDs from my animations and pasted it in now the animations are working. Well that’s good. Also, it still doesn’t show the sword on my back until I equip it and unequip it.

Edit 2: Now the idle animation is stuck.
image

Sorry it took me awhile; I re-adjusted your code since a lot of it was confusing or had extra components that were not needed. The sword was on your back still because you kept making multiple copies of it, so the code eventually lost track of which handle to remove. Here is the adjusted code:

local tool = script.Parent
--- Removed the 'handle' variable.



function WeaponLocation()
   	if tool.Parent.Name == "Backpack" then
		local Character = tool.Parent.Parent.Character
		
		if Character then
			local UpperTorso = Character:FindFirstChild("Torso")
			local Weapon = Character:FindFirstChild("Handle")
			
			if UpperTorso and not Weapon then
			        --- The model that you created here was not necessary.

				local handle = tool.Handle:Clone()
				handle.Name = "Handle"
				handle.Parent = player
				
				local LegWeld = Instance.new("Weld")
				LegWeld.Name = "WeldOnLeg"
				LegWeld.Parent = UpperTorso
				LegWeld.Part0 = UpperTorso
				LegWeld.Part1 = handle
				LegWeld.C0 = CFrame.new(0,-0.2,0.5)
				LegWeld.C0 = LegWeld.C0 * CFrame.fromEulerAnglesXYZ(math.rad(0),math.rad(-180),math.rad(-137.12))
			end
		end
	elseif tool.Parent:FindFirstChild("Handle") ~= nil then
		print("Removing weapon.")
		tool.Parent.Handle.Parent = nil
	end
end

tool.Equipped:Connect(function()
	local equip = tool.Parent.Humanoid:LoadAnimation(tool.equip)
	local idle = tool.Parent.Humanoid:LoadAnimation(tool.idle)
	equip:play()
	
	equip.Stopped:Connect(function() --- This fires the code when the animation is done.
		---  idle:Play()
	end)
	
	WeaponLocation() --- Here as well.
end)

tool.Unequipped:Connect(function()
     WeaponLocation()  --- Here is where you look to see where the weapon is.
end)

tool.Activated:Connect(function()
	if not debounce then
		debounce = true
		
		local slash = tool.Parent.Humanoid:LoadAnimation(tool.slash)
		slash:play()
		slash.Stopped:Connect(function()
			debounce = false
		end)
			
	end
	WeaponLocation() --- Here it is again.
end)

What I changed:

  • Instead of script.Parent.Parent… I used tool for less parents being in the script.
  • I removed the model that you created as it was not necessary (or at least from what you have shown me.
  • I removed the handle variable since it was not needed either; it was easily replaced by referencing handle with tool.Handle.

Of course I had to work around and use my own animations to test it, but everything, including the sword being stuck on your back to animations freezing seemed to be fixed.

Hmmm, the sword still isn’t showing on my back. And it isn’t playing the idle animation correctly.