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

How do a npc play a animation when the player is near his foot ?

Asked by 5 years ago

What i want to do is like : The npc is walking toward the player, it will be following the nearest r6 player and will have a slow rotation speed. When the player touch his feets, the npc will play a stomp animation and when his foot will hit the ground, if the player is still touching the npc foots then the player dies. Can someone help me with that ? Thank you and i hope someone can help me.

0
Thank you very much, it really helps me. :) FireStorm67839 2 — 5y

3 answers

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago
01-- Place as server script into NPC model.
02 
03local Players = game:GetService("Players")
04 
05local Main = script.Parent
06local Humanoid = Main:FindFirstChildOfClass("Humanoid")
07 
08local Range = 4 -- Range
09local Check = true -- Debouncer
10 
11local PlayAnimation = function(AnimationID) -- Play Animation Function
12 local Animation = Instance.new("Animation")
13 Animation.AnimationId = ("rbxassetid://%s"):format(AnimationID)
14 
15 local Track = Humanoid:LoadAnimation(Animation) -- Loads Anim
View all 33 lines...

I'm only providing you with the part where when the NPC gets close to a player then it plays an animation, i won't be providing the rest.

Hope this helps, it does work i've already tested.

Ad
Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

Maybe use Humanoid:MoveTo(), and refer to this thread to find the nearest player.

For the stomping part, find or make an animation and try this code (In an NPC):

01-- NOTE: I am unsure if this code would work as I just wrote it in a rush.
02local Animation = Instance.new("Animation")
03Animation.AnimationId = "rbxassetid://IDHERE" -- Your animation ID is the number from the URL. eg. www.roblox.com/library/1234 the ID would be "1234"
04 
05local Humanoid = script.Parent.Humanoid -- Change this to the path to the HUMANOID of the NPC
06local NPC = workspace.NPC -- Change this to the path to the NPC
07 
08NPC["Left Leg"].Touched:Connect(function(plr)
09    Humanoid:LoadAnimation(Animation):Play() -- You can do Animation:Stop() once the NPC is done with the animation
10    plr.Parent.Character:BreakJoints()
11        end
12end)
13-- Pretty much the same but with Right Leg
14NPC["Right Leg"].Touched:Connect(function(plr)
15    Humanoid:LoadAnimation(Animation):Play()
16            wait(2) -- Length of stomping animation
17        plr.Parent.Character:BreakJoints()
18        end
19end)
0
This isn't helpful at all, if you don't know what you're talking about i suggest you stop. User#31525 30 — 5y
0
I will try it. Thanks anyway ! FireStorm67839 2 — 5y
Log in to vote
1
Answered by 5 years ago

Alright, get ready to read a bit.

You're going to get a Region3. What I'd do is:

Step 1: Make a part, called "Radius," inside of the NPC. It must be anchored and CanCollide false.

Step 2: Create a Script inside of the NPC, and make two variables.

1--General definitions--
2local radius = script.Parent:WaitForChild("Radius")
3local Workspace = game.Workspace

Step 3: Now, we're just going to script a few more variables.

01--Region3-specific definitions--
02local position1, position2 = radius.Position - (radius.Size / 2), radius.Position + (radius.Size / 2) -- Creates two points at opposite corners of our radius to help create the region
03local region = Region3.new(position1, position2) -- Creates the actual region
04 
05local debounce = false -- A quick debounce value, only usable locally, that will tell us if the animation is already playing
06local animTrack = nil -- Loads it once, so that we don't make too many expensive calls each second.  You must define your own, since I don't know what you're doing as an animation.
07 
08while debounce == false do
09    wait(1)
10    -- Play your animation (can't really specify exactly which you're using)
11    debounce = true -- Sets debounce to 'true' making it impossible for the animation to play over and over again
12    wait(10)
13    debounce = false -- Resets debounce to 'false' allowing it to be played again.
14end

BUT, there's a catch! That doesn't check if there's a player nearby. Let's make it do that.

Step 4: Add only-if-player-is-nearby requirements.

01while debounce == false do
02    wait(1)
03    for i, v in pairs(Workspace:FindPartsInRegion3(region)) do -- Find all parts in the region
04        if v.Parent ~= script.Parent and v.Name ~= "Baseplate" then -- Check to make sure the part isn't a body part of our NPC, and isn't the Baseplate.
05            -- Play your animation (can't really specify exactly which you're using)
06            print("Playing...")
07            debounce = true -- Sets debounce to 'true' making it impossible for the animation to play over and over again
08            wait(10)
09            debounce = false -- Resets debounce to 'false' allowing it to be played again.
10        end -- End to if statement that verifies it isn't a part of this NPC/the Baseplate.
11    end -- End to for loop that checks for parts
12end

Step 5: Should be good! Hope this helped! Make sure you test it, first, though. If something doesn't work, let me know and I'll review.

Answer this question