Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

how do i make a toggleable animation ?

local Player = game.Players.LocalPlayer
repeat wait() until Player.Character
local char = Player.Character
local hum = char.Humanoid
local Norm = false
local UIS = game:GetService("UserInputService")
local idle = game.Lighting:WaitForChild("Animation"):WaitForChild("Idle")
UIS.InputBegan:Connect(function(Input, Chat)
	if Input.KeyCode == Enum.KeyCode.F and not Chat and hum.Health >= 0 then
		if Norm == false then

			idle:Play()
			Norm = true
		elseif Norm == true then

			idle:Stop()
			Norm = false
		end
	end
end)

this is the script but it doesnt work i was wondering what is wrong with it

1 comment

0

Please be more specific. What doesn't work?

1 answer

✓ Accepted answer

When playing an animation you need to play the animation either using :LoadAnimation on the humanoid object or playing the animation with the help of an AnimationController.

I cleaned up the code for you, and made the variables a lot more understandable. Please feel free to edit if it doesn't fit your needs.

-- Services
local userInputService = game:GetService("UserInputService")
local lightningService = game:GetService("Lighting")

local animation = lightningService:WaitForChild("Animation")
local idle = animation:WaitForChild("Idle")

-- Player
local player = game.Players.LocalPlayer
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")

local animationTrack = humanoid:LoadAnimation(idle)
local animationPlaying = false

userInputService.InputBegan:Connect(function(Input, Chat)
    if Input.KeyCode == Enum.KeyCode.F and not Chat and humanoid.Health >= 0 then
        if not animationPlaying then

            animationTrack:Play()
            animationPlaying = true
        elseif animationPlaying then

            animationTrack:Stop()
            animationPlaying = false
        end
    end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.