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.
01 | -- Place as server script into NPC model. |
02 |
03 | local Players = game:GetService( "Players" ) |
04 |
05 | local Main = script.Parent |
06 | local Humanoid = Main:FindFirstChildOfClass( "Humanoid" ) |
07 |
08 | local Range = 4 -- Range |
09 | local Check = true -- Debouncer |
10 |
11 | local 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 |
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.
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. |
02 | local Animation = Instance.new( "Animation" ) |
03 | Animation.AnimationId = "rbxassetid://IDHERE" -- Your animation ID is the number from the URL. eg. www.roblox.com/library/1234 the ID would be "1234" |
04 |
05 | local Humanoid = script.Parent.Humanoid -- Change this to the path to the HUMANOID of the NPC |
06 | local NPC = workspace.NPC -- Change this to the path to the NPC |
07 |
08 | NPC [ "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 |
12 | end ) |
13 | -- Pretty much the same but with Right Leg |
14 | NPC [ "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 |
19 | end ) |
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-- |
2 | local radius = script.Parent:WaitForChild( "Radius" ) |
3 | local Workspace = game.Workspace |
Step 3: Now, we're just going to script a few more variables.
01 | --Region3-specific definitions-- |
02 | local position 1 , position 2 = radius.Position - (radius.Size / 2 ), radius.Position + (radius.Size / 2 ) -- Creates two points at opposite corners of our radius to help create the region |
03 | local region = Region 3. new(position 1 , position 2 ) -- Creates the actual region |
04 |
05 | local debounce = false -- A quick debounce value, only usable locally, that will tell us if the animation is already playing |
06 | local 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 |
08 | while 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. |
14 | end |
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.
01 | while debounce = = false do |
02 | wait( 1 ) |
03 | for i, v in pairs (Workspace:FindPartsInRegion 3 (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 |
12 | end |
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.