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

How to stop Animation? Anybody know how to stop animation if tool removed from player backpack?

Anybody know how to stop animation if tool removed from player backpack?

local humanoid = nil
local char = nil
local player = nil
local rootPart = nil
local mouse = nil
local cam = workspace.CurrentCamera
local uis = game:GetService("UserInputService")
local idleAnim = nil
local ForwardAnim = nil
local LeftAnim = nil
local RightAnim = nil


local wPressed = false
local sPressed = false
local aPressed = false
local dPressed = false
local spPressed = false
local equipped = false

local flying = false
script.Parent.Equipped:Connect(function()
	humanoid = script.Parent.Parent.Humanoid
	player = game.Players:GetPlayerFromCharacter(script.Parent.Parent)
	char = script.Parent.Parent
	idleAnim = char:WaitForChild("Humanoid"):LoadAnimation(script:WaitForChild("IdleAnim"))
	ForwardAnim = char:WaitForChild("Humanoid"):LoadAnimation(script:WaitForChild("ForwardAnim"))
	RightAnim = char:WaitForChild("Humanoid"):LoadAnimation(script:WaitForChild("ForwardAnim"))
	LeftAnim = char:WaitForChild("Humanoid"):LoadAnimation(script:WaitForChild("ForwardAnim"))
	rootPart = script.Parent.Parent.HumanoidRootPart
	mouse = player:GetMouse()
	equipped = true
	idleAnim:Play()
end)

script.Parent.Unequipped:Connect(function()
	equipped = false
	idleAnim:Stop()
	ForwardAnim:Stop()
	LeftAnim:Stop()
	RightAnim:Stop()
end)

script.Parent.Remove:Connect(function()
	equipped = false
	idleAnim:Stop()
	ForwardAnim:Stop()
	LeftAnim:Stop()
	RightAnim:Stop()
end)

uis.InputBegan:Connect(function(key, chat)
	if equipped then
		if chat then return end	
		if key.KeyCode == Enum.KeyCode.W and equipped == true then
			wPressed = true
			ForwardAnim:Play()
		elseif key.KeyCode == Enum.KeyCode.S and equipped == true then
			sPressed = true
			ForwardAnim:Play()
		elseif key.KeyCode == Enum.KeyCode.A and equipped == true then
			aPressed = true
			LeftAnim:Play()
		elseif key.KeyCode == Enum.KeyCode.D and equipped == true then
			dPressed = true
			RightAnim:Play()
		end
	end

end)

uis.InputEnded:Connect(function(key, chat)
	if chat then return end
	if key.KeyCode == Enum.KeyCode.W then
		wPressed = false
		ForwardAnim:Stop()
	elseif key.KeyCode == Enum.KeyCode.S then
		sPressed = false
		ForwardAnim:Stop()
	elseif key.KeyCode == Enum.KeyCode.A then
		aPressed = false
		LeftAnim:Stop()
	elseif key.KeyCode == Enum.KeyCode.D then
		dPressed = false
		RightAnim:Stop()
	end
end)

2 comments

0

It's my first question so it's so bad

0

Code block updated.

1 answer

Please use code blocks correctly (

-- test

) And don't use Humanoid:LoadAnimation, use Humanoid.Animator:LoadAnimation I don't really understand but you could use: (Remove the --[[]] if you aren't trying to remove the function.)

player.Backpack.ChildRemoved:Connect(function(child)
	local anim = ANIM - your animation to remove
	local toolName = TOOLNAME -- your tool name, ex. if it was "Bat" then put "Bat".
	if child.Name == toolName then
		anim:Stop()
	end
end
--[[
(if you want to remove the connection or remove the function)
local connection = player.Backpack.ChildRemoved:Connect(function(child)
	local anim = ANIM - your animation to remove
	local toolName = TOOLNAME -- your tool name, ex. if it was "Bat" then put "Bat".
	if child.Name == toolName then
		anim:Stop()
	end
end
if [condition] then
	connection:Disconnection()
end
--]]

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