Swimming Animation Not Playing

Swimming Animation not playing when in water.

Lua
local character = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local swimAnimationId = "rbxassetid://16361131542" -- Replace with your actual animation ID
local swimAnimationTrack

local waterPart = workspace.Water -- Reference your water part

-- Function to start swimming animation
local function startSwimmingAnimation()
    if swimAnimationTrack and swimAnimationTrack.IsPlaying then 
        return -- Animation already playing
    end 

    swimAnimationTrack = humanoid:LoadAnimation(swimAnimationId)
    swimAnimationTrack.Priority = Enum.AnimationPriority.Action 
    swimAnimationTrack:Play()
end

-- Function to stop swimming animation
local function stopSwimmingAnimation()
    if swimAnimationTrack and swimAnimationTrack.IsPlaying then
        swimAnimationTrack:Stop()
    end
end

-- Detect when the player enters the water
game:GetService("RunService").RenderStepped:Connect(function()  
    local characterBottom = character.PrimaryPart.Position.Y - (character.PrimaryPart.Size.Y/2)
    local isSwimming = characterBottom < waterPart.Position.Y + (waterPart.Size.Y/2) 

    if isSwimming then
        startSwimmingAnimation() 
    else
        stopSwimmingAnimation()
    end
end)

If you’re using the Roblox terrain water, you can use the player’s character’s humanoid’s HumanoidStateType.

local players = game:GetService("Players")
local player = players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local hum = character:WaitForChild("Humanoid")

local function onNewState(oldState, newState)
    if newState == Enum.HumanoidStateType.Swimming then
        --do stuff because they are swimming
    else
        --do stuff because they aren't swimming
    end
end

hum.StateChanged:Connect(onNewState)

If you aren’t using the default Roblox water, you can use the .Touched event to cause them to swim.

yourWaterPart.Touched:Connect(function(hit)
    local hum = hit.Parent:FindFirstChild("Humanoid")
    if hum then
        if hum:GetState() ~= Enum.HumanoidStateType.Swimming then
            hum:ChangeState(Enum.HumanoidStateType.Swimming)
        end
    end
end)