Attempt to index nil with Character need help

So I was scripting and I have a module script where i have a table with animations that will play at random once a function is ran, everything was working until I added some changes to this module script and now nothing works. Even if i try to take away my changes I get the error “Attempt to index nil with character” Ive tried to add a wait, a waitforchild() and CharacterAdded:Wait() instead of just player.Character.

 Player = game.Players.LocalPlayer
 Character = Player.Character or Player.CharacterAdded:Wait()
local module = {
	SprintAnim = Character.Humanoid:LoadAnimation(game.ReplicatedStorage.Specss.Run);
	BlockAnim = Character.Humanoid:LoadAnimation(game.ReplicatedStorage.Specss.BlockingAnim);

	["HitAnims"] = {
		HitOne = Character.Humanoid:LoadAnimation(game.ServerScriptService:WaitForChild("Combat").Hit.One);
		HitTwo=  Character.Humanoid:LoadAnimation(game.ServerScriptService:WaitForChild("Combat").Hit.Two);


return module

This is the script, please help!

also reason i used global variable is because i thought it would work if i just changed that but it didnt

1 Like

What is the line number? Can you post the entire error?


Its line 3

I assume you are using a ServerScript to require the module.
The reason for your error is that Players.LocalPlayer only runs on LocalScripts. To make this module work on the server, you can create a function within the module where you pass the player as an argument and then load the animations.
Also, Humanoid:LoadAnimation is deprecated. I suggest looking into using Animators.

I changed it and made a playeradded event, but now it says attempt to index Nil with LoadAnimation()

To clarify, here’s a quick example:


local Module = {}

local testAnimation ="Animation")
testAnimation.AnimationId = "ANIMATION_ID"

local animations = {
	TestAnimation = testAnimation,

function Module:loadAnimations(player)
	local character = player.Character or player.CharacterAdded:Wait()
	local humanoid = character:WaitForChild("Humanoid")
	local animator = humanoid:FindFirstChildOfClass("Animator")

	local animationTracks = {}

	for name, animation in animations do
		animationTracks[name] = animator:LoadAnimation(animation)

	return animationTracks

return Module


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

local Module = require(PATH_TO_MODULE)

	local animationTracks = Module:loadAnimations(player)
	-- // Sometime later:

Yeah, thank you man. It works now

1 Like