Animation plays after you put down your sword

hey , so recently i got a new member (finally :D),i’m not experienced Scripter already telling, and i have a lot of bugs going on in my game, i’m not sure if i can post all the bugs in one post , i’ll make separate post (or should i do it in one post?)
ok, so the first bug in my game (that i wanted to fix it so much) is animation bug

so what i’m trying to achieve is making the animation play when the player presses LMB
but what’s happens is…
https://gyazo.com/e9de97e61c205d0e1b2409e46aa2b843

(in this GIF i’m just taking my sword out, pressing mouse [animation doesn’t plays] , but when putting it in , the animation plays)

my script:

local animations = {"3492887169"}
local tool = script.Parent
local enabled = true
local char

tool.Activated:connect(function()
	if enabled then
		enabled = false
		local char = tool.Parent
		local random = animations[math.random(1,#animations)]
		
		local anim = Instance.new("Animation")
		anim.AnimationId = "http://www.roblox.com/asset/?id="..random
		
		local track = char.Humanoid:LoadAnimation(anim)
		track:Play()
		
		local dmg = script.Damage:Clone()
		dmg.Parent = tool.Handle
		dmg.Disabled = false
		wait(track.Length)
		
		enabled = true
		if dmg then
			dmg:Destroy()
		end
	end
end)

(if needed here’s a picture where it placed)

so that’s all, if you guys can recommend me how to make my posting much cleaner, i’ll appreciate it , thanks in advance :slight_smile:

1 Like

I’m no pro, but I don’t think “random” is set to your animation?

i think it would give me errors if so, but it doesn’t gives me errors…

local animations = {“3492887169”}
local tool = script.Parent
local enabled = true
local char
local anim

tool.Activated:connect(function()
if enabled then
enabled = false
local char = tool.Parent
local random = animations[math.random(1,#animations)]

    anim = Instance.new("Animation")
	anim.AnimationId = "http://www.roblox.com/asset/?id="..random
	
	local track = char.Humanoid:LoadAnimation(anim)
	track:Play()
	
	local dmg = script.Damage:Clone()
	dmg.Parent = tool.Handle
	dmg.Disabled = false
	wait(track.Length)
	
	enabled = true
	if dmg then
		dmg:Destroy()
	end
end

end)
tool.Unequipped:Connect(function()
if anim ~= nil then
anim:Stop()
end
end)
That should stop the animation when you unequip it. As for your random script, it is fine because you are finding the random of 1 between 1 in a table, which is 1 :laughing: This is for your unequipping problem.

And sorry for my indenting and code format.

it gives error says “Stop not valid member of Animation” in line 32 which is the anim:Stop()

Hey, I recently just gotten invited as a New Member too, like 3 minutes ago actually and I thought I would solve this problem for you. You see, the animation track has a property called IsPlaying. Basically if animTrack.IsPlaying then you would destroy it. Simple enough. I’ll implement into code.

    local animation = Instance.new(“Animation”)
    animation.AnimationId = ID

    local animTrack
    tool.Activated:Connect(function()
           animTrack = humanoid:LoadAnimation(animation)
           animTrack:Play()
    end)
    
    tool.Unequipped:Connect(function()
           if animTrack then
                 if animTrack.IsPlaying then
                       animTrack:Stop()
                 end
                 —[[Tbh with you, I recommed
                 just destroying the animTrack after you
                 check if it exists instead of 
                 checking if it’s playing.—]]
           end
    end)

That was just an example on how you would implement it. Hope it helps.

You get that error because animation track has the function Stop() and not just animation.

local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://3492887169"

local animTrack
	local tool = script.Parent
	local char
tool.Activated:Connect(function()
	local char = tool.Parent
       animTrack = char.Humanoid:LoadAnimation(animation)
       animTrack:Play()
	local track = char.Humanoid:LoadAnimation(animation)
	track:Play()
	
	local dmg = script.Damage:Clone()
	dmg.Parent = tool.Handle
	dmg.Disabled = false
	wait(track.Length)
	if dmg then
		dmg:Destroy()
	end
end)

tool.Unequipped:Connect(function()
       if animTrack then
             if animTrack.IsPlaying then
                   animTrack:Stop()
             end
       end
end)

i did something like this, it doesn’t gives me errors but it doesn’t works either

Maybe just try destroying the animTrack after you check if it exists.

how i check if it exist? and do i replace is with the “enabled = true” thing

Well you already did. It was the if animTrack then statement. After that you want to do animTrack:Destroy(). So remove the part that check if animTrack.IsPlaying.

like this?

tool.Unequipped:Connect(function()
       if animTrack then
				animTrack:Destroy()
       end
end)

It is supposed to be track:Stop() and track is the one that is a local variable outside. You should call the animation outside of the activated and create it before, so that it can play when you want it to.

Yes. That is correct. So that should work.

local animation = Instance.new(“Animation”)
animation.AnimationId = “rbxassetid://3492887169”

local animTrack
local tool = script.Parent
local char
tool.Activated:Connect(function()
local char = tool.Parent
animTrack = char.Humanoid:LoadAnimation(animation)
animTrack:Play()
local dmg = script.Damage:Clone()
dmg.Parent = tool.Handle
dmg.Disabled = false
wait(track.Length)
if dmg then
dmg:Destroy()
end
end)

tool.Unequipped:Connect(function()
if animTrack then
animTrack:Stop()
end
end)
This should work.

it gives error in the wait(track.Length) says attempt to index global ‘track’

Change track.Length to animTrack.Length

already tried , doesn’t give errors but still won’t work