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

Punching mechanic works but not in game?

So I've designed a punching mechanic that triggers when you press 'E'; I haven't programmed damage yet, only an animation occurs, but I tested it in game and it didn't work. It works in Studio, what did I do wrong? The error that I get does not show up in studio, but rather in the output bar in-game. "Humanoid is not a valid member of Model"

local player = game:GetService'Players'.LocalPlayer
local char = player.Character or player.CharacterAdded:wait()
local Hum = char.Humanoid -- This is the Humanoid the error is referring to.
local animation = Instance.new("Animation")

animation.AnimationId = "rbxassetid://855470374" -- Punching Animation 

game:GetService("UserInputService").InputBegan:connect(function(Key)
     if Key.KeyCode == Enum.KeyCode.E then
	
local animTrack = Hum:LoadAnimation(animation) 
animTrack:Play() -- Initiate the Punching Animation

	 end  
end) 

Initially there was a problem with Char with it loading before the player did, which might somehow be correlated with this issue. If anyone could give any insight on how to fix this, I would be extremely grateful.

8 comments

0

I'm currently working on a My Hero Academia game solo, and this mechanic is (obviously) needed in pretty much any RPG. I'm about to shamelessly advertise my game, but uh... Check it out? https://www.roblox.com/games/823313848/Project-Horizon-v1-0-Early-Alpha

0

Is the script a LocalScript or Script? Where is the script located?

0

Localscript. Its located under StarterGui and then ScreenGui (for now)

2

The humanoid object may not have loaded at the time the script executed: try using the WaitForChild function and see what happens. :) (Parent:WaitForChild(Object_Name))

0

I don't understand what you mean- At least fully- But I'll try it!

0

Yeahhhhh... I don't understand what you mean. Sorry, I'm kind of an amateur programmer ;-;

0

If you could post an Answer explaining what you mean more in-depth, that would help a lot! Thank you for your help so far, though!

0

bump

1 answer

✓ Accepted answer

http://wiki.roblox.com/index.php?title=API:Class/Instance/WaitForChild

When any script begins, it can't be sure anything is loaded. The "character" you get from CharacterAdded is merely an empty model at that moment. Use WaitForChild for any object that isn't a direct parent of the script.

Also it is bad practice to load the animation every single time. You should do it beforehand.. but you have to wait() or else the humanoid is not ready.

Lastly all of these objects might be useless once the player dies; consider using StarterCharacterScripts.

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:wait()
local Hum = char:WaitForChild("Humanoid")

local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://855470374" -- Punching Animation
wait() 
local animTrack = Hum:LoadAnimation(animation) 

game:GetService("UserInputService").InputBegan:connect(function(Key)
	if Key.KeyCode == Enum.KeyCode.E then
		animTrack:Play() -- Initiate the Punching Animation
	end 
end) 

1 comment

0

Thank you so much! I seriously can't thank you enough! I understood much better this time. Overall, I knew I had to do WaitForChild with Humanoid, but I just didn't understand where to place it. Also, thanks for the tip! I'll make sure to utilize it in the future!

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