Hello,
I’m working on a script to morph an NPC into any player’s avatar when a username is typed in chat. This is part of a test project using the Roblox FPS System template for background mechanics. This is not intended for official publishing.
-
What do I want to achieve?
I want the NPC to morph into a player’s avatar on command, and when it dies, it should respawn at the same position with full health and the same avatar. -
What is the issue?
After the NPC dies and the respawn logic runs, the health bar does not restore properly, the NPC looks dead and can’t be “revived,” even though the script sets health to max. I’m including a video & screenshot attachment below to show this behavior.
Here’s a screenshot for those who cannot see the video.
-
What solutions have I tried?
I’ve usedhumanoid:ApplyDescription()
and reset health withhumanoid.Health = humanoid.MaxHealth
inside a respawn function. I animate the NPC’s name during respawn to show a “Respawning…” message. I’ve searched the Creator Hub and DevForum but haven’t found a solution that fully restores health without recreating the NPC. -
The script in question with explained parts:
local npc = script.Parent
local humanoid = npc:FindFirstChildOfClass("Humanoid")
local Players = game:GetService("Players")
local currentlyMorphing = false
local currentUsername = npc.Name
local rememberedDescription = nil
-- Morph NPC into a user's avatar and remember the description
local function morphToUser(username)
if currentlyMorphing then return end
currentlyMorphing = true
local success, userId = pcall(function()
return Players:GetUserIdFromNameAsync(username)
end)
if not success then
warn("Username not found:", username)
currentlyMorphing = false
return
end
local success2, description = pcall(function()
return Players:GetHumanoidDescriptionFromUserId(userId)
end)
if not success2 then
warn("Failed to get avatar description for", username)
currentlyMorphing = false
return
end
humanoid:ApplyDescription(description)
npc.Name = username
currentUsername = username
rememberedDescription = description
currentlyMorphing = false
end
-- Animate the name while waiting to respawn
local function animateRespawnName(callback)
local states = { "Respawning", "Respawning.", "Respawning..", "Respawning...", "Respawning....", "Respawning....." }
coroutine.wrap(function()
for _, state in ipairs(states) do
npc.Name = state
task.wait(0.3)
end
callback()
end)()
end
-- Respawn the same NPC using remembered avatar and heal
local function respawnNPC()
local position = npc:GetPrimaryPartCFrame() -- where it died
animateRespawnName(function()
npc:SetPrimaryPartCFrame(position)
if rememberedDescription then
humanoid:ApplyDescription(rememberedDescription)
npc.Name = currentUsername
end
humanoid.Health = humanoid.MaxHealth
end)
end
-- When NPC dies
humanoid.Died:Connect(function()
respawnNPC()
end)
-- Listen for player chat
Players.PlayerAdded:Connect(function(player)
player.Chatted:Connect(function(message)
local username = message:match("^(%w+)$")
if username then
morphToUser(username)
elseif message:lower() == "!fix" then
respawnNPC()
end
end)
end)
I would really appreciate any help or insights on how to fix the health bar so the NPC fully revives after death without needing to recreate the entire model.
Thanks so much!