local connection
local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local RS = game:GetService("ReplicatedStorage")
local idle = humanoid:LoadAnimation(script.Idle)
local equip = humanoid:LoadAnimation(script.Equip)
local tool = script.Parent
local active = false
local db = false
character:WaitForChild("Humanoid").Died:Connect(function()
repeat wait(2)
for i,v in pairs(script:GetChildren()) do
if v:IsA("Animation") and v.Name == "Equip" then
equip = player.Character:WaitForChild("Humanoid"):LoadAnimation(v)
end
if v:IsA("Animation") and v.Name == "Idle" then
idle = player.Character:WaitForChild("Humanoid"):LoadAnimation(v)
end
end
until character:WaitForChild("Humanoid")
end)
tool.Equipped:Connect(function()
idle:Play()
active = true
connection = humanoid.Running:Connect(function(speed)
if humanoid.MoveDirection.Magnitude == 0 and active == true then
if not idle then idle = humanoid:LoadAnimation(RS.Idle) end
if equip then equip:Stop() end
idle:Play()
else
if idle then idle:Stop() end
equip:Play()
end
end)
end)
tool.Unequipped:Connect(function()
connection:Disconnect()
equip:Stop()
active = false
if idle.IsPlaying then
idle:Stop()
end
if equip.IsPlaying then
equip:Stop()
end
end)
As Mango said above, try using :WaitForChild() on both the equip and idle animations. I have had issues where my script also couldn’t find the member, even when it was there, simply because i hadn’t put :WaitForChild()
I just tried that, but it didn’t work it prints running but doesn’t do anything else
local Player = game.Players.LocalPlayer
local Character = Player.CharacterAdded:Wait()
local humanoid = Character:WaitForChild("Humanoid")
local tool = Player.Backpack.Sword or Player.Character:FindFirstChild("Sword")
local connection
local equip = humanoid:LoadAnimation(Player.Backpack:WaitForChild("Sword").LocalScript.Idle)
local Anim = game:GetService("ReplicatedStorage").Animation
PlayAnim = Character.Humanoid:LoadAnimation(Anim)
--start running
UIS.InputBegan:connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift then
print("Running")
humanoid:UnequipTools()
Character.Humanoid.WalkSpeed = 35
game:GetService("StarterGui"):SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
PlayAnim:Play()
humanoid.Jumping:Connect(function(isActive)
if isActive then
PlayAnim:Stop()
wait(0.5)
PlayAnim:Play()
end
end)
end
end)
-- stop running
UIS.InputEnded:connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift then
game:GetService("StarterGui"):SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, true)
Character.Humanoid.WalkSpeed = 16
if PlayAnim.IsPlaying then
PlayAnim:Stop()
else
return
end
end
end)
if humanoid.Died then
if PlayAnim.IsPlaying then
PlayAnim:Stop()
else
return
end
end
When the humanoid dies, a new character is respawned, therefore you’ll have to define that character again. In Player Scripts, the script run once the player joins, while character scripts run every time the character is added, the script in character scripts is “reset” defining the new character and stuff.
Unless you define the new character after it dies from the player scripts, the script wont affect any character since the old character does not exist anymore. Character scripts is the way to go for this though.
scripts in startercharacterscripts reset whenever the character is reloaded i.e dying, whilst scripts in starterplayerscripts only reset when the player is reloaded, which really only happens upon joining the game. so whenever you reset your character the script in starterplayerscript doesn’t detect the change and thus doesn’t update the variable, which is the reason why when you try to play the animation from the script, it calls it a nil value because the character you’re referencing no longer exists
consider marking my answer as the solution so that other people with the same problem know how to fix it