Hello, I’m working on a NPC Service. Currently I have NPC appearance on hold and other things done cause I’ve been focusing on make the NPC’s playing random animations. Which is what this topic is about. I’m trying to get my NPC’s to a random animation but it keeps giving me a invalid argument 2 error.
– Error
– Code
-- Services --
local Players = game:GetService('Players')
local CollectionService = game:GetService('CollectionService')
local PhysicsService = game:GetService('PhysicsService')
-- Variables --
local NPCFolder = workspace:FindFirstChild('NPCs')
local AnimFolder = NPCFolder:FindFirstChild('Animations')
local PlrGroup = 'PlrGroup'
local NPCGroup = 'NPCGroup'
local Increments = 75
local CurrentPart = nil
----
-- Tables --
local Colors = {'Reddish brown','Burnt Sienna','Dark orange','Nougat','Light orange'}
local Tops = {'Pink','Dark stone grey','Camo','Deep blue','Pearl','Really black'} -- Will Get From Kreios
local Bottoms = {'Pink','Dark stone grey','Camo','Deep blue','Pearl','Really black'} -- Will Get From Kreios
----
-- Collision Configuration --
PhysicsService:RegisterCollisionGroup(PlrGroup)
PhysicsService:RegisterCollisionGroup(NPCGroup)
----
-- Collision Setting / For Statement --
for _,Folder in ipairs(NPCFolder.Fans:GetChildren()) do
-- Check --
if Folder:IsA('Folder') then
for _,Model in ipairs(Folder:GetChildren()) do
if Model:IsA('Model') then
-- Check --
for _,Asset in ipairs(Model:GetChildren()) do
if Asset:IsA('BasePart') then
-- Set Groups --
Asset.CollisionGroup = NPCGroup
Asset.Parent.PrimaryPart:SetNetworkOwner(nil)
-- Spawn Statement --
task.spawn(function()
local SkinColor = Colors[math.random(1,#Colors)]
local TopColor = Tops[math.random(1,#Tops)]
local BottomColor = Bottoms[math.random(1,#Bottoms)]
-- Set Colors --
Model['Head'].BrickColor = BrickColor.new(SkinColor)
Model['Torso'].BrickColor = BrickColor.new(TopColor)
Model['Right Arm'].BrickColor = BrickColor.new(SkinColor)
Model['Left Arm'].BrickColor = BrickColor.new(SkinColor)
Model['Right Leg'].BrickColor = BrickColor.new(BottomColor)
Model['Left Leg'].BrickColor = BrickColor.new(BottomColor)
end)
elseif Asset:IsA('Humanoid') then
local Humanoid = Asset
local Torso = Model['Torso']
-- Disabled States --
Humanoid:SetStateEnabled(Enum.HumanoidStateType.Climbing,false)
Humanoid:SetStateEnabled(Enum.HumanoidStateType.Flying,false)
Humanoid:SetStateEnabled(Enum.HumanoidStateType.StrafingNoPhysics,false)
-- Spawn Statement --
task.spawn(function()
while true do
task.wait(5)
Humanoid:MoveTo(Torso.Position + Vector3.new(math.random(-Increments,Increments),0,math.random(-Increments,Increments)),CurrentPart)
end
end)
-- Check --
NPCFolder.Cheer.Changed:Connect(function()
-- Table --
local Anims = {
Clap = AnimFolder.Cheer1,
Jump = AnimFolder.Cheer2,
}
-- Check --
if NPCFolder.Cheer.Value == true then
task.spawn(function()
local SelectedAnim = Humanoid:LoadAnimation(Anims[math.random(1,#Anims)])
local Animation = SelectedAnim.Name
Animation:Play()
end)
end
end)
end
end
end
end
end
end
----
-- On Added --
Players.PlayerAdded:Connect(function(Plr)
Plr.CharacterAppearanceLoaded:Connect(function(Character)
-- For Statement --
for _,Asset in ipairs(Character:GetChildren()) do
if Asset:IsA('BasePart') then
Asset.CollisionGroup = PlrGroup
end
end
end)
end)
----
-- Init --
PhysicsService:CollisionGroupSetCollidable(PlrGroup,NPCGroup,false)
PhysicsService:CollisionGroupSetCollidable(NPCGroup,NPCGroup,false)
----