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)