Can someone please tell me why this script wont detect humanoid

Any help on why this dosent work?

local anim1 = 9573486948
local anim2 = 9573483058
local plr = script.Parent.Parent.Parent.Name
local player = workspace:FindFirstChild(plr)

script.Parent.RemoteEvent.OnServerEvent:Connect(function()
	if script.One.Value == true then
		player.Humanoid:LoadAnimation(anim1)
	end
end)

Using this method is deprecated. Also, please specify what type of script this is so I can further assess. Are there any errors in the output?

Explain what part is deprecated please.
heres the error:Screen Shot 2022-05-07 at 11.50.22 PM

Is this a server script or a local script?

The error seems to indicate that the plr variable is failing.

server ()())()()()()()()()()()

It seems that finding the player is actually unnecessary because you are listening to a RemoteEvent. Simply, fetch the player instance from the argument passed through the event.

local anim1 = 9573486948
local anim2 = 9573483058

script.Parent.RemoteEvent.OnServerEvent:Connect(function(player)
	if script.One.Value == true then
		player.Character.Humanoid:LoadAnimation(anim1)
	end
end) 

Also, what I was describing before, using Humanoid:LoadAnimation() is now deprecated, and it is suggseted that you use Humanoid.Animator:LoadAnimation() instead.

Screen Shot 2022-05-07 at 11.58.38 PM
error

The reason why you’re getting both of those errors is because the parent of the tool changing when it is equipped or unequipped. It is either inside the character or player backpack I’m concluding that you can only use the tool when it is equipped, so instead of this quoted line, you can get the player from the character and then retreive the name with this script:

local plr = script.Parent.Parent:IsA("Model") and game:GetService("Players"):GetPlayerFromCharacter(script.Parent.Parent).Name or script.Parent.Parent.Parent.Name

Alternatively, I’ve made a complete fix to your script error and you can copy it:

local anim1 = 9573486948
local anim2 = 9573483058
local plr = script.Parent.Parent:IsA("Model") and game:GetService("Players"):GetPlayerFromCharacter(script.Parent.Parent) or script.Parent.Parent.Parent

local function LoadAnimation(hum, animationId)
    if hum:IsA("Humanoid") then
        local LoadAnimation = hum:LoadAnimation(animationId)
        return LoadAnimation
    end
end

script.Parent.RemoteEvent.OnServerEvent:Connect(function()
	if script.One.Value == true then
        local Humanoid = plr.Character:FindFirstChild("Humanoid")
        if Humanoid then
            local animation1 = LoadAnimation(Humanoid, anim1)
            -- as an example, to play the animation use animation1:Play()
        end
	end
end)

To load an animation to the Humanoid, use this example line:

local animationExample = LoadAnimation(Humanoid, animationIdExample)

To play animation:

animationExample:Play()

To stop the animation (before finishes playing):

animationExample:Stop()

For more info on how to do stuff with the animationExample variable which is an AnimationTrack just look here to find stuff about it: AnimationTracks

Edit: I noticed that this is a server script because of the OnServerEvent… To play the Animation properly, you will have to use a LocalScript. Maybe try firing the RemoteEvent back to the client and move the LoadAnimation function to a LocalScript in the tool.

All you need to do is:

Open
  • Change Tool Server Script to this:
local plr = script.Parent.Parent:IsA("Model") and game:GetService("Players"):GetPlayerFromCharacter(script.Parent.Parent) or script.Parent.Parent.Parent

script.Parent.RemoteEvent.OnServerEvent:Connect(function()
	if script.One.Value == true then
        script.Parent.RemoteEvent:FireClient(plr)
	end
end)
  • Create LocalScript in the tool with this code:
local anim1 = 9573486948
local anim2 = 9573483058
local plr = game.Players.LocalPlayer

local function LoadAnimation(hum, animationId)
    if hum:IsA("Humanoid") then
        local LoadAnimation = hum:LoadAnimation(animationId)
        return LoadAnimation
    end
end

script.Parent.RemoteEvent.OnClientEvent:Connect(function()
    local Humanoid = plr.Character:FindFirstChild("Humanoid")
    if Humanoid then
        local animation1 = LoadAnimation(Humanoid, anim1)
        -- as an example, to play the animation use animation1:Play()
    end
end)

I’ve made an edit. just go back to the message if you haven’t seen ^^

Its better to use the Animator Instance instead of Humanoid, besides this for loading you require an animation instance not the id. So you need to create an Animation instance with the appropriate id. This has to be done in the server if you want the animation to replicate.

fixed myself. :smiley: fdsfdsffdsfsdf

1 Like