Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Punching mechanic works but not in game?

Asked by 6 years ago

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.

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 OrcaTheFish 95 — 6y
0
Is the script a LocalScript or Script? Where is the script located? shayner32 478 — 6y
0
Localscript. Its located under StarterGui and then ScreenGui (for now) OrcaTheFish 95 — 6y
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)) TheeDeathCaster 2368 — 6y
View all comments (4 more)
0
I don't understand what you mean- At least fully- But I'll try it! OrcaTheFish 95 — 6y
0
Yeahhhhh... I don't understand what you mean. Sorry, I'm kind of an amateur programmer ;-; OrcaTheFish 95 — 6y
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! OrcaTheFish 95 — 6y
0
bump OrcaTheFish 95 — 6y

1 answer

Log in to vote
0
Answered by
cabbler 1942 Moderation Voter
6 years ago
Edited 6 years ago

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) 
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! OrcaTheFish 95 — 6y
Ad

Answer this question