Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

'Humanoid is not a valid member of backpack'? [Solved]

Hey, I'm recently new to Lua, I have a basic understanding of html and so I can understand some things but I'm really stuck on this bit of coding for my game.

This is a localscript for a tool, and I'm trying to make it so when the tool is equipped and you press the 'E' key, it will play an animation and sound. This does work, however when I unequip the tool and press 'E' I get an error saying 'Humanoid is not a valid member of backpack - Line 14', and it breaks the script completely so when i re-equip the tool nothing happens when I press E.

This is really frustrating as it seems to be an easy fix but I can't see anything wrong with my 'local Animation', thanks in advance for any help/advice!

local debounce = false
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://5147845886"
local CrateAnimation = Instance.new("Animation")
CrateAnimation.Parent = script
CrateAnimation.AnimationId = "rbxassetid://5150418068"
UserInputService = game:GetService("UserInputService")

script.Parent.Equipped:connect(function()
	UserInputService.InputBegan:Connect(function(input,gameProccesedEvent)
		if input.KeyCode == Enum.KeyCode.E then
			if not debounce then
				debounce = true
				local Animation = script.Parent.Parent.Humanoid:LoadAnimation(script.Animation)
				Animation:Play()
				sound:Play()
		 		wait(3)
		    	Animation:Stop()
				sound:Stop()
				debounce = false
			end
       end
	end)
end)

Location of script:

StarterPack: Tool: LocalScript, Handle, Animation,

LocalScript is located within the Tool which is located in the StarterPack

2 comments

0

Where is this script located? Can you provide the heirarchy?

0

Oh apologies, I've edited my post now to provide that information. The LocalScript along with the Animation is located within a Tool which is located within the StarterPack. Hope this is what u meant.

1 answer

✓ Accepted answer

You can just check if the tool is equipped and make a variable that we can use later. We can check it like this,

local Equipped = false

tool.Activated:Connect(function()
     Equipped = true
end)

tool.Unequipped:Connect(function()
     Equipped = false
end)

Then we can check if the variable is set to true when they press E.

UIS.InputBegan:Connect(function(Input, GPE)
   if GPE then return end
   if Input.KeyCode == Enum.KeyCode.E and Equipped == true then 
   Sound:Play()
   AnimationTrack:Play()
    end
end)

1 comment

0

I added this to my code and it now works perfectly! Thank you so much!

This discussion is preserved as it was. New answers, comments, and votes are closed.