Custom Animations

Hello! I am trying my best learning how to script by working with my best friend ChatGPT which turns out to have issues too.

I am making custom animations for players when they equip a tool. While the tool is equipped, players get a custom idle animation, a custom walk animation, and a custom sprinting animation when they hold shift on top of holding the tool.

When creating the animations I set the following animation priorities:
Idle: Idle
Walk: Movement
Sprint: Movement

My current issue being the animations not triggering properly as shown in the following video:

My scripting knowledge is not that great but I’m trying my best, I tried spamming ChatGPT as much as I could, but it could not resolve the issue. I am reading through potentially similar issues as I’m publishing this.

Here’s a sample of my code:

local tool = script.Parent
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

-- References to custom animations
local customIdleAnimation = "https://www.roblox.com/asset/?id=000" -- ( I put my animation IDs obviously)
local customWalkingAnimation = "https://www.roblox.com/asset/?id=001"
local customSprintingAnimation = "https://www.roblox.com/asset/?id=002"

local shiftKeyDown = false
local isSprinting = false
local isEquipped = false

local humanoid = character:WaitForChild("Humanoid")

-- Create new animation instances
local customIdle = Instance.new("Animation")
customIdle.AnimationId = customIdleAnimation

local customWalking = Instance.new("Animation")
customWalking.AnimationId = customWalkingAnimation

local customSprinting = Instance.new("Animation")
customSprinting.AnimationId = customSprintingAnimation

-- Assign custom animations
humanoid:LoadAnimation(customIdle).Name = "Idle"
humanoid:LoadAnimation(customWalking).Name = "Walk"

local function playSprintingAnimation()
	humanoid.WalkSpeed = 26
	isSprinting = true
	humanoid:LoadAnimation(customSprinting):Play() 
end

local function resetSprinting()
	humanoid.WalkSpeed = 16 
	isSprinting = false
	humanoid:LoadAnimation(customWalking).Name = "Walk"
end

local function checkInput(input)
	if input.KeyCode == Enum.KeyCode.LeftShift and humanoid:GetState() == Enum.HumanoidStateType.Running then
		if isEquipped then
			if not isSprinting then
				playSprintingAnimation()
			else
				resetSprinting()
			end
		end
	end
end

tool.Equipped:Connect(function()
	isEquipped = true
	resetSprinting()
end)

tool.Unequipped:Connect(function()
	isEquipped = false
	resetSprinting()
end)

game:GetService("UserInputService").InputBegan:Connect(checkInput)
game:GetService("UserInputService").InputEnded:Connect(checkInput)

It would be much appreciated if someone could help me.

Thank you for reading!

2 Likes

For the resetSprinting() function, you have to put this;

local function resetSprinting()
	humanoid.WalkSpeed = 16 
	isSprinting = false
       for _, playingTracks in pairs(humanoid:GetPlayingAnimationTracks()) do
	       playingTracks:Stop()
       end
	local walkAnim1= humanoid:LoadAnimation(customWalking)
        walkAnim1.Name == "Walk"
        walkAnim1:Play()
end

However if that doesn’t work, try removing the walkAnim1:Play()

3 Likes

Yo! Thanks a lot, It did fix the sprinting issue but the walking animation seems to be playing at the same time as the sprinting one now.

3 Likes

Change the playSprintingAnimation() with

local function playSprintingAnimation()
	humanoid.WalkSpeed = 26
	isSprinting = true
for _, playingTracks in pairs(humanoid:GetPlayingAnimationTracks()) do
	       playingTracks:Stop()
       end
	local sprintAnim1= humanoid:LoadAnimation(customSprinting)
        sprintAnim1.Name == "Sprint"
        sprintAnim1:Play()
end

This should work :smiley:

2 Likes

Yoo! It does work but now the idle animation does not trigger lmao

1 Like