I'm making a script when you press some keys it increases some values, but it stops working when the player dies, here's the script

local player = script.Parent.Parent
local name = player.Name
local human1 = workspace:WaitForChild(name)
local human = human1:WaitForChild("Humanoid")
print(name)
local coolE = false
local keyheld = false
local coolQ = false

local stats = game.ReplicatedStorage:WaitForChild("stats"..name)
local power = stats:WaitForChild("power")
local ki = stats:WaitForChild("ki")


local function onKeyPress(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == Enum.KeyCode.E and coolE == false then
		coolE = true
		power.Value = power.Value + 5
		local animation = Instance.new("Animation")
		animation.AnimationId = "http//roblox.com/?id=1312116274"
		local animTrack = human:LoadAnimation(animation)
		animTrack:Play()
		print("more power")
		wait(0.5)
		animTrack:Stop()
		coolE = false
		
		
	elseif inputObject.KeyCode == Enum.KeyCode.Q and coolE == false then
		local animation = Instance.new("Animation")
		coolE = true
		animation.AnimationId = "http//roblox.com/?id=01335430799"
		local animTrack = human:LoadAnimation(animation)
		ki.Value = ki.Value + 5
		print("more ki")
		animTrack:play()
		wait(0.4)
		coolE = false
		animTrack:stop()
		
		
	
		end
		end
	
game:GetService("UserInputService").InputBegan:connect(onKeyPress)

When i die the output shows it: 23:13:21.798 - LoadAnimation requires the Humanoid object (VoltronInhe.Humanoid) to be a descendant of the game object

1 comment

1

I think when the player dies, the script can no longer find the "current" humanoid as the player has respawned, including its humanoid. Try adding a CharacterAdded function into it.