This is in a local script. I was originally having issues with the script not being able to find the char even when I did if not player.Character then player.CharacterAdded:Wait() end so then I put everything inside a onCharAdded Event and that seemed to work except it wouldn’t run on the first spawning so that’s what the if (player.Character ~= nil) and player.Character:WaitForChild("Humanoid").Health ~= 0 then is for. Now I have a different script that I wanted to use :LoadCharacter(), but once again its giving me problems with not being able to load the animation onto the humanoid. So I think at this point I’m definitely setting up something wrong.
function onCharacterAdded(character)
repeat wait() until character:FindFirstChild("Humanoid")--this was added
local humanoid = character:WaitForChild("Humanoid")
local gripAnimation = game.ReplicatedStorage.PlayerAnimations.PistolGrip
local gripTrack = humanoid:WaitForChild("Animator"):LoadAnimation(gripAnimation)
end
if (player.Character ~= nil) and player.Character:WaitForChild("Humanoid").Health ~= 0 then
onCharacterAdded(player.Character)
end
player.CharacterAdded:Connect(onCharacterAdded)
Output Error: LoadAnimation requires the Humanoid object (TheCreatorBenn.Humanoid) to be a descendant of the game object
It’s a local script located inside a tool. I think I started to find the problem. It’s caused by using EgoMoose’s Ragdoll module. I think…
Here’s what I tested.
character.Parent = game.Workspace
repeat wait() until character.Parent == game.Workspace
local humanoid = character:WaitForChild("Humanoid")
print(humanoid.Parent)--this was printing my char's name
print(humanoid.Parent.Parent)--this was printing nil
Once I set character.Parent = game.Workspace then everything worked normally. The only problem now is that it creates a clone of my character.
Well since its Inside a tool you don’t have to do all those methods , tools only exist in the Character once they are alive . You can just get the Character , then the Animator from tis Humanoid Load the animation and assign it to a variable. When the player dies the script regens cause the tool gets regened too. I am unsure about the Egomoose’s module stuffs tho.
The weird thing is that the tool seems to load before the character is even loaded. Im guessing it loads the tool and things are already running before its parented to the character. Because in the end what seemed to fix it all was just putting every function inside the equipped event. So then its guaranteed when those variables are made that the char exists. So I’m not exactly sure now if egomoose’s ragdoll module had any effect on things as much as i thought. I first assumed it was the problem because parenting my char to the workspace made a clone of it. I wish that when you do local char = player.Character in a local script that it’d just work like that and it’d be the right char every time you spawn, but that doesn’t seem to be the case.
To verify this I made this simple script to see what it would do
local tool = script.Parent
local player = game.Players.LocalPlayer
local char = player.Character
tool.Activated:Connect(function()
print(char)
print(char.Parent)
end)
char was nil
next I tested
local tool = script.Parent
local player = game.Players.LocalPlayer
if not player.Character then player.CharacterAdded:Wait() end
local char = player.Character
tool.Activated:Connect(function()
print(char)
print(char.Parent)
end)
This worked, but if I did player:LoadCharacter() then
the results were that char wasn’t nil this time, but char.Parent was nil
If you want to verify these claims feel free to test it. Just that small script inside a tool with a handle.
local player = game.Players.LocalPlayer
tool.Activated:Connect(function()
local char = player.Character
print(char)
print(char.Parent)
end)
In this case the chance of Character being nil would be very low. And inside that you can load the animation and assign to a Variable with few checks like :
local player = game.Players.LocalPlayer
local Animation
tool.Activated:Connect(function()
local char = player.Character
--Define Animator
Animation = Animation or Animator:LoadAnimation(AnimationObject)
end)
Yes that would fix the problem. I originally didn’t think of putting an event inside of another event. So lets say you played an animation when u click down which is tool.Activated, but wanted to stop it when tool.Deactivated. if that animation was put inside of tool.Activated and tool.Deactivated was outside of it then tool.Deactivated wouldn’t be able to stop the animation. So the simple solution would be to put tool.Deactivated also inside of tool.Activated. So I guess moral of the story is that when getting the char inside of a local script its always a good habit to get it within an event like equipped or activated depending on how much it’ll be used by other events.
No as you can see in my code , Animation is defined outside the scope so just do
local player = game.Players.LocalPlayer
local Animation
tool.Activated:Connect(function()
local char = player.Character
--Define Animator
Animation = Animation or Animator:LoadAnimation(AnimationObject)
Animation:Play()
end)
tool.Deactivated:Connect(function()
if Animation then Animation:Stop() end
end)
Also getting the character is totally based on where the Character is located , If its Inside StarterPlayerScripts its good to get it inside an event , if its inside StarterCharacterScripts just do script.Parent.
local tool = script.Parent
local player = game.Players.LocalPlayer
local zapTrack
tool.Activated:Connect(function()
local char = player.Character
local humanoid = char.Humanoid
local zappedAnimation = game.ReplicatedStorage.PlayerAnimations.Zapped
local zapTrack = humanoid:WaitForChild("Animator"):LoadAnimation(zappedAnimation)
zapTrack:Play()
end)
tool.Deactivated:Connect(function()
if zapTrack then zapTrack:Stop() end
end)
I tested with one of my animation tracks and in tool.Deactivated zapTrack is underlined in blue and the hint says “Variable ‘zapTrack’ defined at line 3 was never initialized or assigned; initialize with ‘nil’ to silence” When I went in test mode the looped animation played, but never stopped.