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.
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.
Search “Humanoid” in the Core Content tab and drag & drop the object inside the Client Context you have just created.
Customize the clothes of the Humanoid.
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.
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.
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.
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”.
Drag & Drop the “Humanoid 2 Rig” object in the Custom Properties of the AnimateSeller.
Double-click on the AnimateSeller to open the Script Editor.
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()
functionOnBeginOverlap(trigger, other)ifnot other:IsA("Player") thenreturnend
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.
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()
functionOnBeginOverlap(trigger, other)ifnot other:IsA("Player") thenreturnendif other ~= Game.GetLocalPlayer() thenreturnend
NPC:LookAt(other:GetWorldPosition())
NPC:PlayAnimation("unarmed_wave")
end
TRIGGER.beginOverlapEvent:Connect(OnBeginOverlap)Code language:Lua(lua)
This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.
Strictly Necessary Cookies
Strictly Necessary Cookie should be enabled at all times so that we can save your preferences for cookie settings.
If you disable this cookie, we will not be able to save your preferences. This means that every time you visit this website you will need to enable or disable cookies again.
3rd Party Cookies
This website uses Google Analytics to collect anonymous information such as the number of visitors to the site, and the most popular pages.
Keeping this cookie enabled helps us to improve our website.
Please enable Strictly Necessary Cookies first so that we can save your preferences!