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 4 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 — 4y

3 answers

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago
-- Place as server script into NPC model.

local Players = game:GetService("Players")

local Main = script.Parent
local Humanoid = Main:FindFirstChildOfClass("Humanoid")

local Range = 4 -- Range
local Check = true -- Debouncer

local PlayAnimation = function(AnimationID) -- Play Animation Function
 local Animation = Instance.new("Animation")
 Animation.AnimationId = ("rbxassetid://%s"):format(AnimationID)

 local Track = Humanoid:LoadAnimation(Animation) -- Loads Anim
 Track:Play()   
end

while wait() and Check do
   for _,Player in next, Players:GetPlayers() do
    local Char = Player.Character or Player.CharacterAdded:wait() 
    local HumanoidRootPart = Char:FindFirstChild('HumanoidRootPart')
    local A,B = HumanoidRootPart.Position, Main.HumanoidRootPart.Position

    local Dist = (A - B).Magnitude
    if Dist <= Range then 
      PlayAnimation(180426354)
      Check = false
      wait(5)
      Check = true
     end
   end
end

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 4 years ago
Edited 4 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):

-- NOTE: I am unsure if this code would work as I just wrote it in a rush.
local Animation = Instance.new("Animation")
Animation.AnimationId = "rbxassetid://IDHERE" -- Your animation ID is the number from the URL. eg. www.roblox.com/library/1234 the ID would be "1234"

local Humanoid = script.Parent.Humanoid -- Change this to the path to the HUMANOID of the NPC
local NPC = workspace.NPC -- Change this to the path to the NPC

NPC["Left Leg"].Touched:Connect(function(plr)
    Humanoid:LoadAnimation(Animation):Play() -- You can do Animation:Stop() once the NPC is done with the animation
    plr.Parent.Character:BreakJoints()
        end
end)
-- Pretty much the same but with Right Leg
NPC["Right Leg"].Touched:Connect(function(plr)
    Humanoid:LoadAnimation(Animation):Play() 
            wait(2) -- Length of stomping animation
        plr.Parent.Character:BreakJoints()
        end
end)
0
This isn't helpful at all, if you don't know what you're talking about i suggest you stop. User#31525 30 — 4y
0
I will try it. Thanks anyway ! FireStorm67839 2 — 4y
Log in to vote
1
Answered by 4 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.

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

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

--Region3-specific definitions--
local 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
local region = Region3.new(position1, position2) -- Creates the actual region

local debounce = false -- A quick debounce value, only usable locally, that will tell us if the animation is already playing
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.

while debounce == false do
    wait(1)
    -- Play your animation (can't really specify exactly which you're using)
    debounce = true -- Sets debounce to 'true' making it impossible for the animation to play over and over again
    wait(10)
    debounce = false -- Resets debounce to 'false' allowing it to be played again.
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.

while debounce == false do
    wait(1)
    for i, v in pairs(Workspace:FindPartsInRegion3(region)) do -- Find all parts in the region
        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.
            -- Play your animation (can't really specify exactly which you're using)
            print("Playing...")
            debounce = true -- Sets debounce to 'true' making it impossible for the animation to play over and over again
            wait(10)
            debounce = false -- Resets debounce to 'false' allowing it to be played again.
        end -- End to if statement that verifies it isn't a part of this NPC/the Baseplate.
    end -- End to for loop that checks for parts
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.

Answer this question