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.