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

how would i make a player or npc who got hit play an animation?

so im trying to make a nice combat system for my game but i am a fairly new to scripting,but to further elaborate in my question how would i go about making a player/npc who has been punched by another player play an animation showing that the hit has landed again im pretty new to scripting so my attempt at this is laughable but here it is :

local Player = game.Players.LocalPlayer local Character = Player.Character local c = Player.Character

if Player:TakeDamage(5) -- the amount of damage a single punch does then

 local Track = Instance.new("Animation")
        Track.AnimationId = "rbxassetid://4787043272"
        local Anim = c.Humanoid:LoadAnimation(Track)
        Anim:Play()

end

i put this in StarterPlayer scripts

1 answer

Make sure it is a local script

local player = game.Players.LocalPlayer
local character = player.Character
local hum = character:WaitForChild("Humanoid")

local Track = Instance.new("Animation")
if hum.Health <= 95 and character ~= nil then
       Track.AnimationId = "rbxassetid://4787043272"
       local Anim = hum:LoadAnimation(Track)
       Anim:Play()
end

Put script in NPC and put this:

local character = script.Parent
local hum = character:WaitForChild("Humanoid")

while true do
	wait(0.1) -- cooldown so it doesn't
	-- crash
	if hum.Health <= 95 and character ~= nil then
	local Track = Instance.new("Animation")
     	Track.AnimationId = "rbxassetid://4787043272"
        local Anim = hum:LoadAnimation(Track)
    	Anim:Play()
	break
	end
end

6 comments

0

I'm thinking maybe a while loop might work.

0

if i would want to do this for An npc what would i do then? would i just put this script in the npc and change up a few variables?

0

Yes, because the LocalPlayer wouldn't work.

0

hmm this didn't work out and nothing is showing up in output for the npc

0

Try that. I edited it.

0

Depending on the objective, you can include the "break" or remove it.

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