Trigger and Animation

Adding an Animated Mesh

Animated meshes are assets that can be animated. There are animals and different types of Humanoid. You can define the clothes of the Humanoid in their properties by double-clicking on the silhouette.

  1. Create a new Client Context. We are going to set the Animated Mesh in the Client Context to optimize our game as we don’t need to manage animations on the server.
  2. Search “Humanoid” in the Core Content tab and drag & drop the object inside the Client Context you have just created.
  3. Customize the clothes of the Humanoid.
Creating a new Client Context
Double click on the slot to equip your NPC
Our NPC that is going to sell cosmetics

In this lesson, we want the NPC to look at the player and wave at them when the player is close enough to the NPC. To detect the player, we are going to use a Trigger, which is a zone that fires an event when a player or an object enters or leaves the Trigger. The final result will look like the following GIF.

The NPC looks at the player and waves at them

Detect the player

Drag a Trigger from the Core Content to the ClientContext and place it like on the screenshot. If you can’t see the Trigger, press V. This is the zone where the NPC will detect the player. You must set the Game Collision to Force On on the Trigger to detect the player from a Client Context.

Press V to show the Trigger

Play the animation

To play the animation, we are going to use a script that detects when the player enters the Trigger and then plays the animation. Check once again that you set the Game Collision to Force On on the Trigger to detect the player from a Client Context.

  1. Create a new Script called AnimateSeller and add it as a child of the Trigger, that way we can get the reference to the Trigger in the script using “script.parent”.
  2. Drag & Drop the “Humanoid 2 Rig” object in the Custom Properties of the AnimateSeller.
  3. Double-click on the AnimateSeller to open the Script Editor.
NPC Custom Property and AnimateSeller as a child if the Trigger

This is the code that detects the player, rotates the NPC, and plays the animation.

The first two lines are two different ways to get the reference of an object, either using the Hierarchy with script.parent, or using Custom Properties. The WaitForObject() function is just waiting for the asset to be loaded.

The function OnBeginOverlap is called with the right parameters (reference to the Trigger and reference to the object) when the beginOverlapEvent is fired. This connection is done at the bottom of the script. In the function, the first line checks if the other object is of type Player. If it is not the case, it returns to leave the function. If it is a player, then we can use two functions from the API called LookAt to rotate the NPC to face the position of the Player and PlayAnimation to play a specific animation. The list of all the animations can be found here.

local TRIGGER = script.parent
local NPC = script:GetCustomProperty("NPC"):WaitForObject()

function OnBeginOverlap(trigger, other)
    if not other:IsA("Player") then return end
	
    NPC:LookAt(other:GetWorldPosition())
    NPC:PlayAnimation("unarmed_wave")
end

TRIGGER.beginOverlapEvent:Connect(OnBeginOverlap)Code language: Lua (lua)

If the script is not working, check if the Game Collision of your Trigger is set to Force On and if your script is set as a child of the Trigger.

Play the animation only for the local player

Even if it is a Client Context, it does not mean that it is only played for the current player. Other players will also see the NPC facing the player and waving at him.

Another player arrives and the NPC waves at him

To avoid that, we have to look at the object called “other” in our function and see if this other object is the local player. This script checks that condition before moving the NPC. If the other object is not equal to Game.GetLocalPLayer(), then return to stop the function here.

local TRIGGER = script.parent
local NPC = script:GetCustomProperty("NPC"):WaitForObject()

function OnBeginOverlap(trigger, other)
    if not other:IsA("Player") then return end

    if other ~= Game.GetLocalPlayer() then return end
	
    NPC:LookAt(other:GetWorldPosition())
    NPC:PlayAnimation("unarmed_wave")
end

TRIGGER.beginOverlapEvent:Connect(OnBeginOverlap)Code language: Lua (lua)
The NPC is now doing nothing if another player is coming next to him
Post a comment

Leave a Comment

Scroll to Top