Animation not playing on character

im trying to make it so when a player clicks the keybind X an animation plays that reduces the speed of the opponent in front of them, when i click X the animation doesn’t play but all the print statements come out as true and says the animation is loaded

local Players = game:GetService("Players")

local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local rootPart = character:WaitForChild("HumanoidRootPart")

local BLOCK_KEY = Enum.KeyCode.X
local BLOCK_DURATION = 3 -- Duration of the block effect in seconds
local COOLDOWN_DURATION = 5 -- Cooldown duration in seconds
local BLOCK_RADIUS = 5 -- Radius for block collision detection

local lastBlockTime = 0
local isBlocking = false

-- Load the block animation
local blockAnimation = Instance.new("Animation")
blockAnimation.AnimationId = "http://www.roblox.com/asset/?id=16155202554"

local function onKeyPress(input)
   if input.KeyCode == BLOCK_KEY and not isBlocking and os.time() - lastBlockTime >= COOLDOWN_DURATION then
   	isBlocking = true
   	lastBlockTime = os.time()

   	-- Play block animation
   	local blockAnimationInstance = humanoid:LoadAnimation(blockAnimation)
   	print("Block animation loaded:", blockAnimationInstance)
   	blockAnimationInstance:Play()
   	print("Block animation playing:", blockAnimationInstance.IsPlaying)

   	-- Reduce walk speed of nearby players
   	for _, player in ipairs(game.Players:GetPlayers()) do
   		local char = player.Character
   		if char and char ~= character and (char.PrimaryPart.Position - rootPart.Position).Magnitude <= BLOCK_RADIUS then
   			local humanoid = char:FindFirstChildOfClass("Humanoid")
   			if humanoid then
   				humanoid.WalkSpeed = humanoid.WalkSpeed / 2 -- Reduce walk speed by half
   			end
   		end
   	end

   	-- Wait for the duration of the block effect
   	wait(BLOCK_DURATION)

   	-- Reset speed after duration
   	if isBlocking then
   		humanoid.WalkSpeed = humanoid.WalkSpeed
   		isBlocking = false
   	end

   end
end

local function checkBlockCollision()
   if isBlocking then
   	local parts = workspace:FindPartsInRegion3(
   		Region3.new(rootPart.Position - Vector3.new(BLOCK_RADIUS, BLOCK_RADIUS, BLOCK_RADIUS), 
   			rootPart.Position + Vector3.new(BLOCK_RADIUS, BLOCK_RADIUS, BLOCK_RADIUS)), 
   		nil, math.huge)

   	for _, part in ipairs(parts) do
   		local humanoid = part.Parent:FindFirstChildOfClass("Humanoid")
   		local player = game.Players:GetPlayerFromCharacter(part.Parent)
   		if humanoid and player and player ~= game.Players.LocalPlayer then
   			-- Check if the player is not on the same team
   			local localPlayerTeam = game.Players.LocalPlayer.Team
   			local playerTeam = player.Team
   			if localPlayerTeam and playerTeam and localPlayerTeam ~= playerTeam then
   				-- Reduce walk speed of the player who is blocked
   				humanoid.WalkSpeed = humanoid.WalkSpeed / 2 -- Reduce walk speed by half
   			end
   		end
   	end
   end
end

UserInputService.InputBegan:Connect(onKeyPress)
game.Players.PlayerAdded:Connect(function(player)
   player.CharacterAdded:Connect(function(char)
   	character = char
   	humanoid = char:WaitForChild("Humanoid")
   	rootPart = char:WaitForChild("HumanoidRootPart")
   end)
end)

-- Check for block collision every frame
game:GetService("RunService").Stepped:Connect(checkBlockCollision)
1 Like

Did you get any errors when trying to play the animation? If not please do try making an animation instance in the script before playing it, as i have had problems with playing animations made using Instance.new()

i put an actual animation inside the script with the same id, but same problem

Does this print out as nil? and try to put the animation in replicated storage instead, as putting it in a local-only space means it wont replicate.

1 Like

putting it in replicatedstorage didnt work, and it comes out as “Block animation loaded: Animation”

1 Like

Load all your animations inside humanoid.Animator and not humanoid itself, also i recommend using task.wait() instead of wait()

1 Like

still doesnt work as expected to

1 Like

Maybe you published the animation incorrectly, which should have been published on the account instead you published it to the group, usually this is a common mistake :open_mouth:

2 Likes

i published it to my own account

1 Like

Change "http://www.roblox.com/asset/?id=16155202554’’ to ‘‘rbxassetid://16155202554’’

(this could be the issue)

1 Like

i changed the script instead of using instance.new by putting an animation in replicatedstorage and adding the animation ID there, nothing changed

1 Like

try setting the animations priority type to action

like so:

blockAnimationInstance.Priority = Enum.AnimationPriority.Action

Also good to note that most block animations are 1 frame, and so you MUST have it looped. so please do check that aswell

1 Like

the block animation still doesnt work

1 Like

the problem is probably with the block animation id. Make sure you own it. Also, creating animations in the client-side won’t replicate to the server

block animation - Roblox the animation is owned by me

1 Like