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.
wait(3)
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
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.
local Module = {}
local testAnimation = Instance.new("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")
task.wait()
local animationTracks = {}
for name, animation in animations do
animationTracks[name] = animator:LoadAnimation(animation)
end
return animationTracks
end
return Module
ServerScript:
local ServerScriptService = game:GetService("ServerScriptService")
local Players = game:GetService("Players")
local Module = require(PATH_TO_MODULE)
Players.PlayerAdded:Connect(function(player)
local animationTracks = Module:loadAnimations(player)
-- // Sometime later:
animationTracks.TestAnimation:Play()
end)