The way I dealt with tools was simply using .DecendantAdded
and .DecendantRemoving
events. Tools get parented between Backpack and Character, so they fire DescendantAdded when you equip and DescendantRemoving when you unequip.
This way, you don’t need to poll every single character every single frame! Just handle them when the events fire. This method is nice cuz it’ll handle other things being added to the character too, such as hats or even custom armor meshes.
For deaths, you can simply reuse the dead body parts for the respawned character. This means you don’t need to recreate it, you just move them from the floor back up into the character.
Edit: Made a functional example
Using my [Open Source] ViewportFrame Handler, I whipped together what you’ve asked for.
local ViewportHandler = require(script.ViewportHandler)
local VF = game.Players.LocalPlayer:WaitForChild("PlayerGui"):WaitForChild("ScreenGui").ViewportFrame
VF.CurrentCamera = workspace.CurrentCamera
local VF_H = ViewportHandler.new(VF)
local pHs = {} --player handlers
game.Players.PlayerRemoving:Connect(function(plr)
if pHs[plr] then
pHs[plr]:Destroy() -- Clear old body
end
end)
game.Players.PlayerAdded:Connect(function(plr) -- Setup players that join later
--Initial
local char = plr.Character or plr.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid",3)
if hum then wait() -- wait otherwise we run this before the meshes load properly
pHs[plr] = VF_H:RenderHumanoid(char)
end
--Future
plr.CharacterAdded:Connect(function(char)
local hum = char:WaitForChild("Humanoid",3)
if hum then wait() -- wait otherwise we run this before the meshes load properly
if pHs[plr] then
pHs[plr]:Destroy() -- Clear old body
end
pHs[plr] = VF_H:RenderHumanoid(char)
end
end)
end)
wait(1) -- Let players load in
for i,plr in pairs(game.Players:GetPlayers()) do -- Setup players in game
spawn(function() -- Run in parallel since each can yield from the :Wait()
--Initial
local char = plr.Character or plr.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid",3)
if hum then wait() -- wait otherwise we run this before the meshes load properly
pHs[plr] = VF_H:RenderHumanoid(char)
end
--Future
plr.CharacterAdded:Connect(function(char)
local hum = char:WaitForChild("Humanoid",3)
if hum then wait() -- wait otherwise we run this before the meshes load properly
if pHs[plr] then
pHs[plr]:Destroy() -- Clear old body
end
pHs[plr] = VF_H:RenderHumanoid(char)
end
end)
end)
end
Everything is handled inside the module, you just tell it what you want it to do!