Help With Sword

I am trying to make a sword system but I don’t know how to make the animations play. I think the problem is in the function playAnimation but I don’t know what, can someone help me?

This is 6r is that helps

local function playAnimation(animationId)
	local humanoid = sword:FindFirstChild("Humanoid")
	if humanoid then
		humanoid:LoadAnimation(animationId):Play()
	end
end
-- LocalScript

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local sword = script.Parent -- assuming the LocalScript is inside the sword tool

local function playAnimation(animationId)
	local humanoid = sword:FindFirstChild("Humanoid")
	if humanoid then
		humanoid:LoadAnimation(animationId):Play()
	end
end

mouse.Button1Down:Connect(function()
	playAnimation("rbxassetid://15388446655")
end)

mouse.Button2Down:Connect(function()
	playAnimation("rbxassetid://15388455036")
end)

mouse.Button2Up:Connect(function()
	playAnimation("rbxassetid://15388460473")
end)
2 Likes

The Humanoid is a child of the Character, not of the tool.

The condition if sword:FindFirstChild("Humanoid") will always fail, no?

1 Like

Maybe instead try making declaring humanoid as player.Character:FindFirstChild(“Humanoid”)? Or like waiting for the character to load in first as well so something along the lines of

local players = game:GetService("Players")
local plr = players.LocalPlayer
local Char = plr.Character

local humanoid

while true do
 wait()
 if Char then
  humanoid = Char:FindFirstChild("Humanoid")
  break
 end
end

It doesn’t have to be that long but its just to make it readable and hopefully working

I got it

-- LocalScript

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local sword = script.Parent -- assuming the LocalScript is inside the sword tool

local animations = {
	"rbxassetid://15388455036",
	"rbxassetid://15388446655",
	"rbxassetid://15388460473"
}

local function playRandomAnimation()
	local randomIndex = math.random(1, #animations)
	local animationId = animations[randomIndex]

	local humanoid = player.Character:FindFirstChildOfClass("Humanoid")
	if humanoid then
		local animation = Instance.new("Animation")
		animation.AnimationId = animationId
		local animationTrack = humanoid:LoadAnimation(animation)
		animationTrack:Play()
	end
end

mouse.Button1Down:Connect(function()
	playRandomAnimation()
end)
1 Like

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