Animation Plays When Activated

I’m working on a mining game, and when you click with the pickaxe, a hitbox spawns to destroy the ore. I wanted to add an animation to this but I still can’t seem to figure it out. Here’s a video of what the game looks like so far:

Whenever you click, I want the animation of the pickaxe swinging to play. Heres the script inside the tool:

local Tool = script.Parent
local Handle = Tool.Handle
local Sound = game.Workspace.MiningSounds.OreHit

local Damage = 25
local Cooldown = 1
local Usable = true

local function CreateHitbox()
	
	local Hitbox = Instance.new("Part")
	Hitbox.Parent = game.Workspace
	Hitbox.Name = "Hitbox"
	
	Hitbox.Size = Vector3.new(2.7, 4, 2)
	Hitbox.Anchored = false
	Hitbox.CanCollide = false
	
	Hitbox.CFrame = Handle.CFrame * CFrame.new(-1.4,0,-2)
	
	
	Hitbox.Touched:Connect(function(hit)
		
		local Mineable = hit
		
		if Mineable and Mineable:FindFirstChild("Health") then
			
			Mineable:FindFirstChild("Health").Value -= Damage
			Sound:Play()
		end
	end)
end


Tool.Activated:Connect(function()
	
	if Usable == true then
		
		
		CreateHitbox()

			Usable = false
			wait(Cooldown)
			Usable = true
		end
end)

I’ve tried several ways to make the animation play but I can’t seem to figure it out. All help and suggestions are appreciated thank you :pray:!

Where exactly do you play the animation? There isn’t any mention of an animation in the script.

local Tool = script.Parent
local Handle = Tool.Handle
local Sound = game.Workspace.MiningSounds.OreHit

local Animation = Instance.new("Animation")
Animation.AnimationId = `rbxassetid://{changetoidhere}`
local AnimationTrack

local function ResetAnimation()
    if not game:GetService("Players"):GetPlayerFromCharacter(Tool.Parent) then return end
    AnimationTrack = Tool.Parent.Humanoid.Animator:LoadAnimation(Animation)
end

ResetAnimation()
Tool:GetPropertyChangedSignal("Parent"):Connect(ResetAnimation)

local Damage = 25
local Cooldown = 1
local Usable = true

local function CreateHitbox()
	
	local Hitbox = Instance.new("Part")
	Hitbox.Parent = game.Workspace
	Hitbox.Name = "Hitbox"
	
	Hitbox.Size = Vector3.new(2.7, 4, 2)
	Hitbox.Anchored = false
	Hitbox.CanCollide = false
	
	Hitbox.CFrame = Handle.CFrame * CFrame.new(-1.4,0,-2)
	
	
	Hitbox.Touched:Connect(function(hit)
		
		local Mineable = hit
		
		if Mineable and Mineable:FindFirstChild("Health") then
			
			Mineable:FindFirstChild("Health").Value -= Damage
			Sound:Play()
		end
	end)
end


Tool.Activated:Connect(function()
	
	if Usable == true then
		
		
		CreateHitbox()

			Usable = false
            AnimationTrack:Play()
			task.wait(Cooldown)
			Usable = true
		end
end)

theres an animation inside the tool named Animation. i should have mentioned that

local Tool = script.Parent
local Handle = Tool.Handle
local Sound = game.Workspace.MiningSounds.OreHit

local Animation = Tool.Animation
local AnimationTrack

local function ResetAnimation()
    if not game:GetService("Players"):GetPlayerFromCharacter(Tool.Parent) then return end
    AnimationTrack = Tool.Parent.Humanoid.Animator:LoadAnimation(Animation)
end

ResetAnimation()
Tool:GetPropertyChangedSignal("Parent"):Connect(ResetAnimation)

local Damage = 25
local Cooldown = 1
local Usable = true

local function CreateHitbox()
	
	local Hitbox = Instance.new("Part")
	Hitbox.Parent = game.Workspace
	Hitbox.Name = "Hitbox"
	
	Hitbox.Size = Vector3.new(2.7, 4, 2)
	Hitbox.Anchored = false
	Hitbox.CanCollide = false
	
	Hitbox.CFrame = Handle.CFrame * CFrame.new(-1.4,0,-2)
	
	
	Hitbox.Touched:Connect(function(hit)
		
		local Mineable = hit
		
		if Mineable and Mineable:FindFirstChild("Health") then
			
			Mineable:FindFirstChild("Health").Value -= Damage
			Sound:Play()
		end
	end)
end


Tool.Activated:Connect(function()
	
	if Usable == true then
		
		
		CreateHitbox()

			Usable = false
            AnimationTrack:Play()
			task.wait(Cooldown)
			Usable = true
		end
end)

it works, but the animation doesnt stop playing (its looping)

For future reference, documentation for AnimationTrack.

local Tool = script.Parent
local Handle = Tool.Handle
local Sound = game.Workspace.MiningSounds.OreHit

local Animation = Tool.Animation
local AnimationTrack

local function ResetAnimation()
    if not game:GetService("Players"):GetPlayerFromCharacter(Tool.Parent) then return end
    AnimationTrack = Tool.Parent.Humanoid.Animator:LoadAnimation(Animation)
    AnimationTrack.Looped = false
end

ResetAnimation()
Tool:GetPropertyChangedSignal("Parent"):Connect(ResetAnimation)

local Damage = 25
local Cooldown = 1
local Usable = true

local function CreateHitbox()
	
	local Hitbox = Instance.new("Part")
	Hitbox.Parent = game.Workspace
	Hitbox.Name = "Hitbox"
	
	Hitbox.Size = Vector3.new(2.7, 4, 2)
	Hitbox.Anchored = false
	Hitbox.CanCollide = false
	
	Hitbox.CFrame = Handle.CFrame * CFrame.new(-1.4,0,-2)
	
	
	Hitbox.Touched:Connect(function(hit)
		
		local Mineable = hit
		
		if Mineable and Mineable:FindFirstChild("Health") then
			
			Mineable:FindFirstChild("Health").Value -= Damage
			Sound:Play()
		end
	end)
end


Tool.Activated:Connect(function()
	
	if Usable == true then
		
		
		CreateHitbox()

			Usable = false
            AnimationTrack:Play()
			task.wait(Cooldown)
			Usable = true
		end
end)
2 Likes

just tested it and that finally works. thank you so much!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.