You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
I Want To Make The Guitar Tool That When Activate Tool It Plays Random Music And Animation, And When You Activate Tool Again, It Stop Animation And Music.
What is the issue? Include screenshots / videos if possible!
The Guitar Tool Itself Plays The Animation And Sound Normally, But When I Activate Tool Again, It Stop Music, But Animation Still Playing, Also There Is None Of Error In Outputs.
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I Looked Devforum That Has Same Issue, And Try Fix By Using These Solution, But None Of Them Worked.
Script That Is In Tool
local player = script.Parent.Parent.Parent
local char = player.Character or player.CharacterAdded:Wait()
local hum = char:FindFirstChild("Humanoid")
local soundfolder = game.ServerStorage.Guitar
playing = false
script.Parent.Activated:Connect(function()
local animation = hum:LoadAnimation(script.Animation)
local randommusic = math.random(1, 2)
if randommusic == 1 and playing == false then
local banjoboogie = soundfolder.BanjoBoogie:Clone()
banjoboogie.Parent = script.Parent.Handle
banjoboogie:Play()
animation:Play()
playing = true
elseif randommusic == 2 and playing == false then
local western = soundfolder.Western:Clone()
western.Parent = script.Parent.Handle
western:Play()
animation:Play()
playing = true
elseif playing == true then
local foundmusic1 = script.Parent.Handle:FindFirstChild("BanjoBoogie")
local foundmusic2 = script.Parent.Handle:FindFirstChild("Western")
if foundmusic1 then
foundmusic1:Destroy()
animation:Stop() ---Not Working!
playing = false
elseif foundmusic2 then
foundmusic2:Destroy()
animation:Stop() ---Not Working!
playing = false
end
end
end)
First of all it wouldve been easier to use animation.IsPlaying. It would be a huge time saver for you. The other thing is, animations not stopping is very common. But after a while I found out that when you create the animation you have to make sure to uncheck looped.
Sorry If I Misunderstand What You Says, But It Didn’t Worked, I Also Added Print, And Print Not Appear On Output.
elseif playing == true then
local foundmusic1 = script.Parent.Handle:FindFirstChild("BanjoBoogie")
local foundmusic2 = script.Parent.Handle:FindFirstChild("Western")
if foundmusic1 and animation.IsPlaying == true then
foundmusic1:Destroy()
animation:Stop()
playing = false
print("stopped1")
elseif foundmusic2 and animation.IsPlaying == true then
foundmusic2:Destroy()
animation:Stop()
playing = false
print("stopped2")
end
end
end)
I Added Print, And It Print Only I Activate Tool So It Not Constantly Being Activated.
script.Parent.Activated:Connect(function()
print("tool activated!")
local animation = hum:LoadAnimation(script.Animation)
local randommusic = math.random(1, 2)
if randommusic == 1 and playing == false then
local banjoboogie = soundfolder.BanjoBoogie:Clone()
banjoboogie.Parent = script.Parent.Handle
banjoboogie:Play()
animation:Play()
playing = true
elseif randommusic == 2 and playing == false then
local western = soundfolder.Western:Clone()
western.Parent = script.Parent.Handle
western:Play()
animation:Play()
playing = true
elseif playing == true then
local foundmusic1 = script.Parent.Handle:FindFirstChild("BanjoBoogie")
local foundmusic2 = script.Parent.Handle:FindFirstChild("Western")
if foundmusic1 and animation.IsPlaying == true then
foundmusic1:Destroy()
animation:Stop()
playing = false
print("stopped1")
elseif foundmusic2 and animation.IsPlaying == true then
foundmusic2:Destroy()
animation:Stop()
playing = false
print("stopped2")
end
end
end)
ok the only thing I have left to change is to remove the playing variable and replace it with is playing. Here i’ve done it for you:
You also had two elseif’s in a row. Your not supposed to do that. Only one else if, and then another if statement.
script.Parent.Activated:Connect(function()
print("tool activated!")
local animation = hum:LoadAnimation(script.Animation)
local randommusic = math.random(1, 2)
if randommusic == 1 and playing == false then
local banjoboogie = soundfolder.BanjoBoogie:Clone()
banjoboogie.Parent = script.Parent.Handle
banjoboogie:Play()
animation:Play()
elseif randommusic == 2 and playing == false then
local western = soundfolder.Western:Clone()
western.Parent = script.Parent.Handle
western:Play()
animation:Play()
if playing == true then
local foundmusic1 = script.Parent.Handle:FindFirstChild("BanjoBoogie")
local foundmusic2 = script.Parent.Handle:FindFirstChild("Western")
if foundmusic1 and animation.IsPlaying == true then
foundmusic1:Destroy()
animation:Stop()
playing = false
print("stopped1")
elseif foundmusic2 and animation.IsPlaying == true then
foundmusic2:Destroy()
animation:Stop()
playing = false
print("stopped2")
end
end
end
end)
the animation isn’t stopping because you have the animation defined in the activated call.
every time you activate the tool, you’re essentially creating a whole new animation every time. this means that when you start playing the guitar it’ll play correctly, but when you stop, the script tries to stop the new animation you just made, instead of the one you previously played.
you should instead move the local animation = hum:LoadAnimation(script.Animation) line above the activation function. that way, the animation is only created once, and every activation will use the same animation object.
do animation = “rbxassetid://INSERTANIMATIONID” or animation = script.animation. When we load the animation that just means its ready to play for the player, otherwise were just constantly loading it everytime like NobleReign said. I somehow missed that
Ignore everything I just said just put local animation above the activate function.
local player = script.Parent.Parent.Parent
local char = player.Character or player.CharacterAdded:Wait()
local hum = char:FindFirstChild("Humanoid")
local soundfolder = game.ServerStorage.Guitar
playing = false
local animation; --have the animation be empty when the script starts up for the first time
script.Parent.Activated:Connect(function()
if not animation then --if there's no animation yet, we should try and load it now
animation = hum:LoadAnimation(script.Animation)
end
local randommusic = math.random(1, 2)
if randommusic == 1 and playing == false then
local banjoboogie = soundfolder.BanjoBoogie:Clone()
banjoboogie.Parent = script.Parent.Handle
banjoboogie:Play()
animation:Play()
playing = true
elseif randommusic == 2 and playing == false then
local western = soundfolder.Western:Clone()
western.Parent = script.Parent.Handle
western:Play()
animation:Play()
playing = true
elseif playing == true then
local foundmusic1 = script.Parent.Handle:FindFirstChild("BanjoBoogie")
local foundmusic2 = script.Parent.Handle:FindFirstChild("Western")
if foundmusic1 then
foundmusic1:Destroy()
animation:Stop() ---Not Working!
playing = false
elseif foundmusic2 then
foundmusic2:Destroy()
animation:Stop() ---Not Working!
playing = false
end
end
end)
i read up a little on the error and it seems like it might be caused by trying to load the animation too quickly. this code should instead have the animation be empty, but then load and store it once the player first tries to play the guitar. this should give roblox enough time to prepare the animation clip service for use, hopefully.