We currently have NPCs in our game.
The Animate
script is using the most resources and causing the most lag by far:
Is there any way to make this script a local script without animations breaking?
We currently have NPCs in our game.
The Animate
script is using the most resources and causing the most lag by far:
Looping through nearby NPCs and running animations for them seems like the best way to do this.
If you need an example, just let me know
Yes, an example would be useful
Okay!
Keep in mind this is just an example, you will obviously have to edit it for your game
It’s also not finished, you will need to add based on what your npcs can do
(I also haven’t tested it, so I don’t know if it works. Don’t remove your server sided animate until you have verified this works!)
local npcs = workspace.NPCs -- Replace with wherever your NPCs are stored
local char = game:GetService("Players").LocalPlayer.Character -- Get the player character (for LoD)
local animsTable = {
runAnim = "rbxassetid:something", -- lol don't actually use this assetid
}
local setupAnims = {} -- Create the table of set up animations for animators
local distanceToRender = 128 -- How far away should animations be rendered (client-side)?
local function setupNPC(npc : Model?)
if npc:IsA("Model") then -- This specific technique will only work if the NPC is a model
local pp = npc.PrimaryPart -- Get the primaryPart
if char.PrimaryPart.Position - pp.Position < distanceToRender then -- Make sure NPCs are close enough
local humanoid = npc:FindFirstChildWhichIsA("Humanoid") -- Get the humanoid
if humanoid then -- Make sure we have a humanoid, we can't animate without one (animators are usually in humanoids)
local animator = humanoid:FindFirstChildWhichIsA("Animator") -- Get the animator
if animator then -- We can't animate without an animator
setupAnims[animator] = {} -- Set up the table for this specific animator
local runAnimation = Instance.new("Animation") -- Create the animation instance for loading later
runAnimation.AnimationId = animsTable.runAnim -- Set the animation id
local runAnimTrack = animator:LoadAnimation(runAnimation) -- Load the animation
runAnimTrack.Looped = true -- Loop the run animation
setupAnims[animator].Run = runAnimTrack -- Store the animation in the dictionary for future reference
humanoid.Running:Connect(function(speed) -- Try to run the animation whenever the humanoid moves
if humanoid.FloorMaterial and humanoid.FloorMaterial ~= Enum.Material.Air and speed > humanoid.WalkSpeed / 2 then -- Make sure the humanoid is on the ground to load the run animation. Also make sure we are actually moving.
setupAnims[animator].Run:Play() -- Play the animation
else
setupAnims[animator].Run:Stop() -- Stop the animation
end
end)
end
end
end
end
end
for i, npc in ipairs(npcs:GetChildren()) do
setupNPC(npc)
end
npcs.ChildAdded:Connect(function(npc)
setupNPC(npc)
end)
Had to add magnitude the distance calculation, but works fine. Thanks!
No problem!
I’d also like to say that if you have custom animations that load in (for example, a punch animation), you can create a remote event that loads in the animation (if needed) on the client and plays it, and the server would fire that remote whenever you need to play that animation.
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.