Why always me, script doesn't work

always, animation won’t work im using a local script located in starterpack ,
error: Attempt To index nil with “LoadAnimation()”
script:

local plr = game.Players.LocalPlayer
local char = plr.Character
local animator = char:FindFirstChildOfClass("Animator")
local uis = game:GetService("UserInputService")

uis.InputBegan:Connect(function(inp, gpe) 
	if inp.KeyCode == Enum.KeyCode.E then
		local anim = animator:LoadAnimation(script:WaitForChild("Charge"))
		anim:Play()
	end
end)
1 Like

guys im dumb the animator is in humanoid my bad sorry

1 Like

Your code finds nil, this is because the script triggers BEFORE the character finishes loading in, so it doesn’t find the animations inside your character.

local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")

local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local animator = character:WaitForChild("Animator")

UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
    if gameProcessedEvent then return the end

    if input.KeyCode == Enum.KeyCode.E then
        local chargeAnimation = script:WaitForChild("Charge", 2)  -- Wait up to 2 seconds for the "Charge" animation to load
        if chargeAnimation then
            local anim = animator:LoadAnimation(chargeAnimation)
            anim:Play()
        else
            warn("Charge animation not found or could not be loaded.")
        end
    end
end)

thank you i actually found the problem im the dumbest human alive am i right? xd

Nah it’s fine, don’t worry about it!

1 Like

thanks for your patience brother

i still ran into a problem here is script, when you hold “E” the charge animation plays and when u release “E” the release animation plays but tthen it loops itself

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")   
local animator = humanoid:FindFirstChildOfClass("Animator")
local uis = game:GetService("UserInputService")

uis.InputBegan:Connect(function(inp, gpe) 
	if inp.KeyCode == Enum.KeyCode.E then
		local anim = animator:LoadAnimation(script:WaitForChild("Charge"))
		anim:Play()
	end
end)

uis.InputEnded:Connect(function(inp, gpe)
	if inp.KeyCode == Enum.KeyCode.E then
		local anim = animator:LoadAnimation(script:WaitForChild("Release"))
		anim:Play()
		wait(1)
		anim:Stop()
		end
end)
1 Like

but what is the problem POST MUST BE ATLEAST 30 LETTERS LONG, HAVE YOU TRIED THE :heart: BUTTON?

reread it
11111111111111111111111111

bro like just tell me what the error is :sob:

i can’t tell what the problem is by just glancing over it

NVM DIDN’T SEE EDIT MB

@RedOver133 can i have a video of the problem (please)

here:


as u can see, i held e the first time but not the second time and the animations play themselves

try storing the functions inside a variable, something like connection1 and then disconnecting them at the end of everything via variableName:Disconnect()

1 Like

im not good at that so idk how to

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")   
local animator = humanoid:FindFirstChildOfClass("Animator")
local uis = game:GetService("UserInputService")

local connection1 
local connection2

connection1 = uis.InputBegan:Connect(function(inp, gpe) 
	if inp.KeyCode == Enum.KeyCode.E then
		local anim = animator:LoadAnimation(script:WaitForChild("Charge"))
		anim:Play()
	end
end)

connection2 = uis.InputEnded:Connect(function(inp, gpe)
	if inp.KeyCode == Enum.KeyCode.E then
		local anim = animator:LoadAnimation(script:WaitForChild("Release"))
		anim:Play()
		wait(1)
		anim:Stop()
		end
connection1:Disconnect()
connection2:Disconnect()
end)
1 Like

now none of the anims work maybe ur wrong idk

okay this is honestly just really weird because those animations should not be looping

i’ve worked with UIS before and my animations have never looped like that
are you sure your animations aren’t looped in properties or something???

not looped bro
11111111111111111111

i have fixed it @notsad2 here is the correct script:

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:FindFirstChildOfClass("Animator")
local uis = game:GetService("UserInputService")

local chargeAnim = animator:LoadAnimation(script:WaitForChild("Charge"))
local releaseAnim = animator:LoadAnimation(script:WaitForChild("Release"))

uis.InputBegan:Connect(function(inp, gpe)
	if not gpe then  -- Ensure the event was not handled by the game
		if inp.KeyCode == Enum.KeyCode.E then
			chargeAnim:Play()
		end
	end
end)

uis.InputEnded:Connect(function(inp, gpe)
	if not gpe then  -- Ensure the event was not handled by the game
		if inp.KeyCode == Enum.KeyCode.E then
			chargeAnim:Stop()  -- Stop the charge animation
			releaseAnim:Play()
			wait(1)
			releaseAnim:Stop()
		end
	end
end)
2 Likes

You have to register a keyup, then break it I think.

ah no i already fixed it
11111111111111