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)
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)
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)
try storing the functions inside a variable, something like connection1 and then disconnecting them at the end of everything via variableName:Disconnect()
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)
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)