Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do i stop a looped animation from a different thread?

Hello im looking for stopping the equipped animation so it stops when its unequipped, also this is a server script inside a tool because i followed a tutorial and it said that, but i dont know how to stop it

local tool = script.Parent
local idlea = tool.Handle.Idle
local shoota = tool.Handle.Shoot
local muzzle = tool.Handle.mzzle

tool.Equipped:Connect(function()
	local character = tool.Parent
	local humanoid = character.Humanoid
	local idle = humanoid:LoadAnimation(idlea)
	idle:Play()
end)

tool.Activated:Connect(function()
	local character = tool.Parent
	local humanoid = character.Humanoid
	local shoot = humanoid:LoadAnimation(shoota)
	shoot:Play()
	muzzle.Transparency = 0.2
	wait(0.2)
	muzzle.Transparency = 1	
end)

tool.Unequipped:Connect(function()
	--Stop animation
end)

2 answers

This is how. I'd also recommend not using wait(), use a delay function instead.

local tool = script.Parent
local idlea = tool.Handle.Idle
local shoota = tool.Handle.Shoot
local muzzle = tool.Handle.mzzle

tool.Equipped:Connect(function()
    local character = tool.Parent
    local humanoid = character.Humanoid
    local idle = humanoid:LoadAnimation(idlea)
    idle:Play()
end)


--with wait
tool.Activated:Connect(function()
    local character = tool.Parent
    local humanoid = character.Humanoid
    local shoot = humanoid:LoadAnimation(shoota)
    shoot:Play()
    muzzle.Transparency = 0.2
    wait(0.2)
    muzzle.Transparency = 1
end)

--with delay
tool.Activated:Connect(function()
    local character = tool.Parent
    local humanoid = character.Humanoid
    local shoot = humanoid:LoadAnimation(shoota)
    shoot:Play()
    muzzle.Transparency = 0.2
    delay(0.2, function()
    muzzle.Transparency = 1
end)
end)

tool.Unequipped:Connect(function()
    idle:Stop()
end)

2 comments

0

what ? this wont solve OP the issue, idle is undefind in that scope (in Equipped) and tell me what you would use delay over wait ?

0

Did you even read my question?

local tool = script.Parent
local idlea = tool.Handle.Idle
local shoota = tool.Handle.Shoot
local muzzle = tool.Handle.mzzle
local character = tool.Parent
local humanoid = character.Humanoid
local idle = humanoid:LoadAnimation(idlea) -- idle now exists in this scope

tool.Equipped:Connect(function()
    idle:Play()
end)

tool.Activated:Connect(function()
    local character = tool.Parent
 
    local shoot = humanoid:LoadAnimation(shoota)
    shoot:Play()
    muzzle.Transparency = 0.2
    wait(0.2)
    muzzle.Transparency = 1 
end)

tool.Unequipped:Connect(function()
     idle:Stop() -- now it can acces idle and call the Stop function on it
end)

so basically i instantiated idle in a higher scope so that we can acces it now from the scope in Unequipped

2 comments

0

where i'm supposed to get the humanoid

0

by moving that code section too.. you should learn basics first instead of following random tuts so that you dont understand what you are writing

This discussion is preserved as it was. New answers, comments, and votes are closed.