Help with flashlight script

Hello Devs! I’m making a simple flashlight script with animation but when i want to turn off the flashlight, the animation for the arm does not stop. please help.

SCRIPT :

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local uis = game:GetService(“UserInputService”)

uis.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.F then
if character:WaitForChild(“Flashlight”).Transparency == 1 then
character:WaitForChild(“Humanoid”):LoadAnimation(script.Flashlight):Play()
character:WaitForChild(“Flashlight”).Transparency = 0
else
if input.KeyCode == Enum.KeyCode.F then
if character:WaitForChild(“Flashlight”).Transparency == 0 then
character:WaitForChild(“Humanoid”):LoadAnimation(script.Flashlight):Stop()
character:WaitForChild(“Flashlight”).Transparency = 1
end
end
end
end
end)

This MIGHT work. I don’t deal with animations at all so it may not

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local uis = game:GetService(“UserInputService”)
local Animation = character:WaitForChild(“Humanoid”):LoadAnimation(script.Flashlight)
uis.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.F then
        if character:WaitForChild(“Flashlight”).Transparency == 1 then
            Animation:Play()
            character:WaitForChild(“Flashlight”).Transparency = 0
        else
            if input.KeyCode == Enum.KeyCode.F then
                if character:WaitForChild(“Flashlight”).Transparency == 0 then
                    Animation:Stop()
                    character:WaitForChild(“Flashlight”).Transparency = 1
                end
            end
        end
    end
end)

btw, when posting scripts in the future, please put ``` before and after the code, and ensure it is formatted.

Thanks, It Worked. and sure ill put the ``` next time

It is in 2 different places you’re meaninglessly increasing the size of your script

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local uis = game:GetService("UserInputService")
local flashlighton = false

uis.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.F then
		if character:WaitForChild("FlashLight").Transparency == 1  and flashlighton == false then
			flashlighton = true
			character:WaitForChild("FlashLight").Transparency = 0
			character:WaitForChild("Humanoid"):LoadAnimation(script.Flashlight):Play()
		elseif character:WaitForChild("FlashLight").Transparency == 0 and flashlighton == true then
			character:WaitForChild("FlashLight").Transparency = 1
			flashlighton = false
			for i,v in pairs(player.Character.Humanoid:GetPlayingAnimationTracks()) do
				v:Stop()
			end
		end
	end
end)

Yeah about the LoadAnimation on Humanoid. Thats Deprecated, so soon it wouldnt work anymore. To play save he should use Animator:LoadAnimation().
But like u said u dont touch animations so its not an issue haha