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)