Hello, everyone
I have a problem with my npc OnDied function sometimes the ragdoll working properly and sometimes not.
As you can see in the image npc sometimes frozen in R15 default pose, but the weird behavior is that some of my other NPCs that are not affected by Humanoid:ApplyDescription() has their Ragdoll working properly, unlike those npcs that were affected by the Humanoid:ApplyDescription.
This is my NPC_Logic.lua (OnDeath function that handle the Ragdoll Using CollectionService)
function NpcClass:OnDeath()
if not self.IsAlive then return end
self.IsAlive = false
self.CurrentState = "Dead"
self.Humanoid:ChangeState(Enum.HumanoidStateType.Physics)
self.Humanoid.PlatformStand = true
self.Humanoid.AutoRotate = false
for _, track in ipairs(self.Humanoid:GetPlayingAnimationTracks()) do
track:Stop()
end
if self.HRP then
self.HRP:SetNetworkOwner(nil)
self.HRP.CanCollide = false
end
for _, descendant in ipairs(self.Char:GetDescendants()) do
if descendant:IsA("Motor6D") then
if descendant.Name ~= "Root" then
local socket = Instance.new("BallSocketConstraint")
local a0 = Instance.new("Attachment")
local a1 = Instance.new("Attachment")
a0.CFrame = descendant.C0
a1.CFrame = descendant.C1
a0.Parent = descendant.Part0
a1.Parent = descendant.Part1
socket.Attachment0 = a0
socket.Attachment1 = a1
socket.LimitsEnabled = true
socket.TwistLimitsEnabled = true
socket.Parent = descendant.Parent
end
descendant:Destroy()
end
end
for _, part in ipairs(self.Char:GetDescendants()) do
if part:IsA("BasePart") then
if part.Name == "HumanoidRootPart" then
part.CanCollide = false
elseif part.Parent:IsA("Accessory") then
part.CanCollide = false
else
part.CanCollide = true
end
end
end
if self.HRP then
self.HRP.AssemblyLinearVelocity = self.HRP.AssemblyLinearVelocity + Vector3.new(math.random(-5,5), 5, math.random(-5,5))
end
local creator = self.Humanoid:FindFirstChild("Creator")
if creator and creator.Value then
local player = creator.Value
local reward = self.Char:GetAttribute("Reward") or "0"
local rep = self.Char:GetAttribute("Reputation") or 0
local stat = self.Char:GetAttribute("Status") or "Innocent"
GainModule:TotalKillsAdd(player, "Kills", 1)
GainModule:StreakAdd(player, "Streak", 1)
if reward ~= "0" then GainModule:CoinsAdd(player, reward) end
if player.leaderstats:FindFirstChild("Status") then
local change = (stat == "Innocent") and -tonumber(rep) or tonumber(rep)
GainModule:ReputationAdd(player, change)
end
QuestManager.OnNpcDeath(self.Char, player)
DropManager.CreateValueDrops(self.HRP.Position, 1, player)
PopOutRemote:FireClient(player, "Coins", reward, player.leaderstats.Streak.Value)
self:Say(self.LinesModule.Dead, true)
end
Debris:AddItem(self.Char, 5)
end
And here is my NPC_Controller (that spawns in NPC into their areas and ApplyDescription).
local function applyAppearance(npc, isFriendMode)
if not isFriendMode then return end
task.spawn(function()
local allPlayers = Players:GetPlayers()
if #allPlayers == 0 then return end
local randomPlayer = allPlayers[math.random(#allPlayers)]
local success, pages = pcall(function() return Players:GetFriendsAsync(randomPlayer.UserId) end)
if success and pages then
local friendList = {}
for _, friend in ipairs(pages:GetCurrentPage()) do table.insert(friendList, friend) end
if #friendList > 0 then
local randomFriend = friendList[math.random(#friendList)]
local descSuccess, humanoidDesc = pcall(function() return Players:GetHumanoidDescriptionFromUserId(randomFriend.Id) end)
if descSuccess and humanoidDesc and npc:FindFirstChild("Humanoid") then
npc.Humanoid:ApplyDescription(humanoidDesc)
npc.Name = randomFriend.Username
npc:SetAttribute("DisplayName", randomFriend.Username)
end
end
end
end)
end
local function spawnNpc(config, difficulty, npcData)
local npcTemplate = NPCS_TEMPLATES_FOLDER:FindFirstChild(npcData.TemplateName)
if not npcTemplate then return warn("Missing Template: " .. npcData.TemplateName) end
local newNpc = npcTemplate:Clone()
local attributes = {
NPCType = npcData.NPCType, Temperament = npcData.Temperament, Difficulty = difficulty, Reward = npcData.Reward,
DisplayName = npcData.TemplateName, Strength = npcData.Strength, Endurance = npcData.Endurance,
Psychic = npcData.Psychic, Speed = npcData.Speed, HasDamageReflection = npcData.HasDamageReflection,
Status = npcData.Status, Reputation = npcData.Reputation
}
for k, v in pairs(attributes) do newNpc:SetAttribute(k, v) end
newNpc.Name = npcData.TemplateName
local enduranceBN = toBN(npcData.Endurance)
local hpMultiBN = toBN(HP_PER_ENDURANCE)
local maxHealthBN = enduranceBN * hpMultiBN
newNpc:SetAttribute("CurrentHealth", tostring(maxHealthBN))
local areaTag = Instance.new("ObjectValue")
areaTag.Name = "SpawnArea"; areaTag.Value = config.SpawnPart; areaTag.Parent = newNpc
local areaSize = config.SpawnPart.Size
local cframe = config.SpawnPart.CFrame
local rx, rz = math.random(-areaSize.X/2, areaSize.X/2), math.random(-areaSize.Z/2, areaSize.Z/2)
newNpc:SetPrimaryPartCFrame(cframe * CFrame.new(rx, 3, rz))
for _, part in ipairs(newNpc:GetDescendants()) do
if part:IsA("BasePart") then part.CollisionGroup = "Characters" end
end
local hum = newNpc:FindFirstChildOfClass("Humanoid")
if hum then
hum.MaxHealth = 100; hum.Health = 100
hum.WalkSpeed = 16 + ((tonumber(npcData.Speed) or 0) / 5)
end
newNpc.Parent = config.NpcsFolder
if newNpc.PrimaryPart then
newNpc.PrimaryPart:SetNetworkOwner(nil)
end
applyAppearance(newNpc, npcData.ApplyFriendAppearance)
CollectionService:AddTag(newNpc, "EnemyNPC")
end
for i, config in ipairs(SPAWN_CONFIG) do
if not config.NpcsFolder.Parent then config.NpcsFolder.Parent = NPCS_PARENT_FOLDER end
spawnTimers[i] = {}
for difficulty, _ in pairs(config.NpcTypes) do
spawnTimers[i][difficulty] = 0
end
end
