Tool animation does not play when called for first time

  1. What do you want to achieve? The animation to play when you first swing your sword instead of it only working.

  2. What is the issue?
    The animation doesn’t play when you first swing your sword and will only play after you swing your sword for the first time.

  3. What solutions have you thought of so far?
    PreloadAsync but it doesn’t seem to be working

Tool = script.Parent
Handle = Tool:WaitForChild("Handle")

function Create(ty)
	return function(data)
		local obj = Instance.new(ty)
		for k, v in pairs(data) do
			if type(k) == 'number' then
				v.Parent = obj
			else
				obj[k] = v
			end
		end
		return obj
	end
end

cooldown = false

local BaseUrl = "rbxassetid://"

Players = game:GetService("Players")
Debris = game:GetService("Debris")
RunService = game:GetService("RunService")

DamageValues = {
	BaseDamage = script.Parent.Damage.Value,
	SlashDamage = script.Parent.Damage.Value,
	LungeDamage = script.Parent.Damage.Value
}


Damage = DamageValues.BaseDamage

--[[Grips = {
	Up = CFrame.new(0, 0, -1.70000005, 0, 0, 1, 1, 0, 0, 0, 1, 0),
	Out = CFrame.new(0, 0, -1.70000005, 0, 1, 0, 1, -0, 0, 0, 0, -1)
}--]]

Sounds = {
	Slash = Handle:WaitForChild("SwordSlash"),
	Lunge = Handle:WaitForChild("SwordLunge"),
	Unsheath = Handle:WaitForChild("Unsheath")
}

canAttack = true
ToolEquipped = false

--For Omega Rainbow Katana thumbnail to display a lot of particles.
for i, v in pairs(Handle:GetChildren()) do
	if v:IsA("ParticleEmitter") then
		v.Rate = 20
	end
end

Tool.Enabled = true

function IsTeamMate(Player1, Player2)
	return (Player1 and Player2 and not Player1.Neutral and not Player2.Neutral and Player1.TeamColor == Player2.TeamColor)
end

function TagHumanoid(humanoid, player)
	local Creator_Tag = Instance.new("ObjectValue")
	Creator_Tag.Name = "creator"
	Creator_Tag.Value = player
	Debris:AddItem(Creator_Tag, 2)
	Creator_Tag.Parent = humanoid
end

function UntagHumanoid(humanoid)
	for i, v in pairs(humanoid:GetChildren()) do
		if v:IsA("ObjectValue") and v.Name == "creator" then
			v:Destroy()
		end
	end
end

function Blow(Hit)
	--	if cooldown == true then
	if not Hit or not Hit.Parent or not CheckIfAlive() or not ToolEquipped then
		return
	end
	local RightArm = Character:FindFirstChild("Right Arm") or Character:FindFirstChild("RightHand")
	if not RightArm then
		return
	end
	local RightGrip = RightArm:FindFirstChild("RightGrip")
	if not RightGrip or (RightGrip.Part0 ~= Handle and RightGrip.Part1 ~= Handle) then
		return
	end
	local character = Hit.Parent
	if character == Character then
		return
	end
	local humanoid = character:FindFirstChild("Enemy")
	if not humanoid or humanoid.Health == 0 then
		return
	end
	local player = Players:GetPlayerFromCharacter(character)
	if player and (player == Player or IsTeamMate(Player, player)) then
		return
	end
	UntagHumanoid(humanoid)
	TagHumanoid(humanoid, Player)

	if canAttack == true then
		local player = Tool.Parent.Name
		canAttack = false
		wait()
		if not humanoid.Parent.Name.find(humanoid.Parent.Name, "Chest") then
			if Players[player].Variables.EquippedPerk.Value == "Poison" then
				if not humanoid.Parent:FindFirstChild("Poison") then
					local poisonScript = game:GetService("ServerStorage").PerkScripts.Poison:Clone()
					poisonScript.Parent = humanoid.Parent
					poisonScript.Player.Value = Tool.Parent.Name
					poisonScript.DPS.Value = Tool.Damage.Value / 100 * 25
				end
			end
			humanoid:TakeDamage(Damage)
			if humanoid.Health <= 0 then
				Players[player].leaderstats.Souls.Value = Players[player].leaderstats.Souls.Value + math.random(
					humanoid.Parent.Information.SoulsMin.Value, humanoid.Parent.Information.SoulsMax.Value)
				Players[player].leaderstats.Kills.Value = Players[player].leaderstats.Kills.Value + 1
			end
			spawn(function()
				if Damage >= 1 then
					local damageIndicator = game:GetService("ServerStorage").DamageIndicator:Clone()
					damageIndicator.Parent = humanoid.Parent.Head
					damageIndicator.ExtentsOffset = Vector3.new(math.random(0, 1), math.random(-1.5, 1.25), math.random(0, 1))
					damageIndicator.Amount.Text = '-' .. Damage
					wait(1)
					damageIndicator:Destroy()
				end
			end)
			wait(script.Parent.UsageDelay.Value)
			canAttack = true
		else
			humanoid:TakeDamage(Damage)
			if humanoid.Health <= 0 then
				local player = Tool.Parent.Name
				Players[player].leaderstats.Gems.Value = Players[player].leaderstats.Gems.Value + math.random(
					humanoid.Parent.Information.GemsMin.Value, humanoid.Parent.Information.GemsMax.Value)
			end
			spawn(function()
				if Damage >= 1 then
					if humanoid.Parent.Name.find(humanoid.Parent.Name, "Chest") then
						local damageIndicator = game:GetService("ServerStorage").DamageIndicator:Clone()
						damageIndicator.Parent = humanoid.Parent.DamagePart
						damageIndicator.ExtentsOffset = Vector3.new(math.random(0, 1), math.random(0, 1), math.random(0, 1))
						damageIndicator.Amount.Text = '-' .. Damage
						wait(1)
						damageIndicator:Destroy()
					end
				end
			end)
			wait(script.Parent.UsageDelay.Value)
			canAttack = true			
		end
	end	
end
--end


function Attack()
	Damage = DamageValues.SlashDamage
	Sounds.Slash:Play()

	if Humanoid then
		if Humanoid.RigType == Enum.HumanoidRigType.R6 then
			local Anim = Instance.new("StringValue")
			Anim.Name = "toolanim"
			Anim.Value = "Slash"
			Anim.Parent = Tool
		elseif Humanoid.RigType == Enum.HumanoidRigType.R15 then
			local Anim = Tool:FindFirstChild("R15Slash")
			if Anim then
				local Track = Humanoid:LoadAnimation(Anim)
				Track:Play(0)
			end
		end
	end	
end

function Lunge()
	Damage = DamageValues.LungeDamage

	Sounds.Lunge:Play()

	if Humanoid then
		if Humanoid.RigType == Enum.HumanoidRigType.R6 then
			local Anim = Instance.new("StringValue")
			Anim.Name = "toolanim"
			Anim.Value = "Lunge"
			Anim.Parent = Tool
		elseif Humanoid.RigType == Enum.HumanoidRigType.R15 then
			local Anim = Tool:FindFirstChild("R15Lunge")
			if Anim then
				local Track = Humanoid:LoadAnimation(Anim)
				Track:Play(0)
			end
		end
	end	
	--[[
	if CheckIfAlive() then
		local Force = Instance.new("BodyVelocity")
		Force.velocity = Vector3.new(0, 10, 0) 
		Force.maxForce = Vector3.new(0, 4000, 0)
		Debris:AddItem(Force, 0.4)
		Force.Parent = Torso
	end
	]]

	Damage = DamageValues.SlashDamage
end

Tool.Enabled = true
LastAttack = 0

function Activated()
	if cooldown == false then
		cooldown = true
		Tool.Handle.Trail.Enabled = true
		spawn(function()
			wait(0.5)
			Tool.Handle.Trail.Enabled = false
		end)
		if not Tool.Enabled or not ToolEquipped or not CheckIfAlive() then
			return
		end
		Tool.Enabled = false
		local Tick = RunService.Stepped:wait()
		if (Tick - LastAttack < 0.2) then
			Lunge()
		else
			Attack()
		end
		LastAttack = Tick
		--wait(0.5)
		Damage = DamageValues.BaseDamage
		local SlashAnim = (Tool:FindFirstChild("R15Slash") or Create("Animation"){
			Name = "R15Slash",
			AnimationId = BaseUrl .. 10619914647,
			Parent = Tool
		})

		local LungeAnim = (Tool:FindFirstChild("R15Lunge") or Create("Animation"){
			Name = "R15Lunge",
			AnimationId = BaseUrl .. 10619914647,
			Parent = Tool
		})
		Tool.Enabled = true
		spawn(function()
			wait(script.Parent.UsageDelay.Value)
			cooldown = false
		end)
		local randomNumber = math.random(1, 3)
		if randomNumber == 1 then
			SlashAnim.AnimationId = BaseUrl .. 10697271861
			LungeAnim.AnimationId = BaseUrl .. 10697271861
		elseif randomNumber == 2 then
			SlashAnim.AnimationId = BaseUrl .. 10697286920
			LungeAnim.AnimationId = BaseUrl .. 10697286920
		elseif randomNumber == 3 then
			SlashAnim.AnimationId = BaseUrl .. 10697288557
			LungeAnim.AnimationId = BaseUrl .. 10697288557
		end
	end
end

function CheckIfAlive()
	return (((Player and Player.Parent and Character and Character.Parent and Humanoid and Humanoid.Parent and Humanoid.Health > 0 and Torso and Torso.Parent) and true) or false)
end

function Equipped()
	Character = Tool.Parent
	Player = Players:GetPlayerFromCharacter(Character)
	Humanoid = Character:FindFirstChildOfClass("Humanoid")
	Torso = Character:FindFirstChild("Torso") or Character:FindFirstChild("HumanoidRootPart")
	if not CheckIfAlive() then
		return
	end
	ToolEquipped = true
	Sounds.Unsheath:Play()
end

function Unequipped()
	ToolEquipped = false
end

Tool.Activated:Connect(Activated)
Tool.Equipped:Connect(Equipped)
Tool.Unequipped:Connect(Unequipped)

Connection = Handle.Touched:Connect(Blow)
1 Like