local tool = script.Parent
local debounce = script.Parent.debounce
local uis = game:GetService('UserInputService')
local debounce2 = true
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local animationTrack = char:WaitForChild('Humanoid'):LoadAnimation(script.Parent:WaitForChild('light_swing_anim')) -- errors
local animationTrack2 = char:WaitForChild('Humanoid'):LoadAnimation(script.Parent:WaitForChild('heavy_swing_anim')) -- errors
tool.Activated:Connect(function()
if debounce.Value and debounce2 and game.Players.LocalPlayer.Character.Humanoid.Health > 0 then
animationTrack:Play()
game.ReplicatedStorage.events.swordevent:FireServer('hit','light')
end
end)
script.Parent.Equipped:Connect(function()
equipped = true
end)
script.Parent.Unequipped:Connect(function()
equipped = false
end)
uis.InputBegan:Connect(function(input)
if equipped then
if debounce.Value and debounce2 then
if input.UserInputType == Enum.UserInputType.MouseButton2 and debounce.Value and game.Players.LocalPlayer.Character.Humanoid.Health > 0 then
if game.Players.LocalPlayer.Character.Humanoid.Health > 0 and game.Players.LocalPlayer.Parent ~= nil then
animationTrack2:Play()
game.ReplicatedStorage.events.swordevent:FireServer('hit','heavy')
end
end
end
end
end)
Error occurs when a player dies and respawns. It is in a local script inside a tool in the starter pack. Works fine until player dies
also i recommend to you use animator, instead of humanoid for loading animation
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animationInstance1 = script.Parent:WaitForChild("light_swing_anim")
local animationInstance2 = script.Parent:WaitForChild("heavy_swing_anim")
local animationTrack1 = animator:LoadAnimation(animationInstance1)
local animationTrack2 = animator:LoadAnimation(animationInstance2)
The error you’re seeing is because char is being set to nil when the player dies and respawns. This is because char is being set to plr.Character , which is nil when the player is dead.
local tool = script.Parent
local debounce = script.Parent.debounce
local uis = game:GetService('UserInputService')
local debounce2 = true
local plr = game.Players.LocalPlayer
local char = plr.CharacterAdded:Wait()
local animationTrack
local animationTrack2
try
animationTrack = char:WaitForChild('Humanoid'):LoadAnimation(script.Parent:WaitForChild('light_swing_anim'))
catch
warn("Could not load light swing animation")
end
try
animationTrack2 = char:WaitForChild('Humanoid'):LoadAnimation(script.Parent:WaitForChild('heavy_swing_anim'))
catch
warn("Could not load heavy swing animation")
end
tool.Activated:Connect(function()
if debounce.Value and debounce2 and game.Players.LocalPlayer.Character.Humanoid.Health > 0 then
animationTrack:Play()
game.ReplicatedStorage.events.swordevent:FireServer('hit','light')
end
end)
script.Parent.Equipped:Connect(function()
equipped = true
end)
script.Parent.Unequipped:Connect(function()
equipped = false
end)
uis.InputBegan:Connect(function(input)
if equipped then
if debounce.Value and debounce2 then
if input.UserInputType == Enum.UserInputType.MouseButton2 and debounce.Value and game.Players.LocalPlayer.Character.Humanoid.Health > 0 then
if game.Players.LocalPlayer.Character.Humanoid.Health > 0 and game.Players.LocalPlayer.Parent ~= nil then
animationTrack2:Play()
game.ReplicatedStorage.events.swordevent:FireServer('hit','heavy')
end
end
end
end
end)
This should fix the error and allow the player to use the tool after respawning.