Hello , so i’ve been looking for a script thatdestroy the character mesh of a player
when his avatar spawns
i’m currently using this script but it takes a second to destroy the CharacterMesh
game.Players.PlayerAdded:Connect(function(p)
p.CharacterAdded:Connect(function(c)
wait(1)
local l = c:GetChildren()
for i = 1 , #l do
local l = l[i]
if l :IsA("CharacterMesh") then
l:Destroy()
end
end
end)
end)
game.Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function(char)
wait()
for i,v in pairs(char:GetChildren()) do
if v:IsA("CharacterMesh") then
v:Destroy()
end
end
end)
end)
Remove the wait(1) statement and before you’ve gone through your loop and destroyed the meshes add a .ChildAdded event to check when a CharacterMesh is added.
Edit:
game:GetService("Players").PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function(char)
char.ChildAdded:Connect(function(ch)
if ch:IsA("CharacterMesh") then
ch:Destroy()
end
end)
for _, ch in ipairs(char:GetChildren()) do
if ch:IsA("CharacterMesh") then
ch:Destroy()
end
end
end)
end)
game.Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function(char)
wait()
Char.ChildAdded:Connect(function(child)
if child:IsA("CharacterMesh") then
child:Destroy()
end
end)
end)
end)
local Players = game.Players
function PlayerJoined(Player)
local function RemoveMeshes(Character)
local Humanoid = Character:WaitForChild("Humanoid")
wait()
local CurrentDescription = Humanoid:GetAppliedDescription() -- gets the current applied description
-- changes all of the body parts description to 0 (default)
CurrentDescription.Head = 0
CurrentDescription.Torso = 0
CurrentDescription.LeftArm = 0
CurrentDescription.RightArm = 0
CurrentDescription.LeftLeg = 0
CurrentDescription.RightLeg = 0
Humanoid:ApplyDescription(CurrentDescription) -- we only changes the body parts so the rest stays the same
end
Player.CharacterAdded:Connect(RemoveMeshes)
end
Players.PlayerAdded:Connect(PlayerJoined)
this works instantly when a player joins and when they reset
you could also change the scale with this method too if you wanted
Why’re you guys spoon feeding code with no after-explanation as to what was changed or giving him learning resources? There’s no point if OP doesn’t learn.
One thing I’ve noticed, first off: your code block hasn’t been formatted properly. When posting code to the DevForum, put your code between a code block. You can create that by using three back ticks.
```lua
-- Your code
```
print("Hello world!")
Next thing I noticed is, well, the code itself. Here are some issues I’ve observed with it:
You are using an unnecessary wait statement to reinvent the wheel, where functions are already provided for you. You can change this to use CharacterAppearanceLoaded and wait until the signal is fired, which tells the script that the avatar’s assets have loaded. On the other hand, since this doesn’t fire for blank characters, you can use ChildAdded and destroy CharacterMesh class children when they’re added.
You’re using the wrong type of for loop; try using a key-value loop instead of via index (since the index is unimportant) [1]
Try indexing your services as a local variable at the top of your script first, via GetService (GetService is a canonical way of fetching services)
You can use the HumanoidDescription system to preserve a certain style for the avatar to look, by applying a 0 value for each limb; you can then apply this HumanoidDescription to the character via a script in StarterCharacterScripts
[1]:
for _, object in pairs(Character:GetChildren()) do
print(object, object.ClassName) -- Replace with your own code
end
OP is trying to remove bundles, not overwrite the entire character. StarterCharacter isn’t used for this purpose; it’s for when you want to overall change the way a character appears, functions or even how it’s assembled. HumanoidDescriptions or reformatting the default character if you’re only aiming to purge a certain type of asset are sufficient enough.
The process is still fairly the same but the methodology behind it is different. I don’t use the index part or re-enter the model hierarchy via index since knowing a numerical order is not necessary - you’ll notice right below your for entry statement that you still need to index a child local l = l[i]. This could also be problematic if the array shifts down (which iirc it does, but I can’t remember).
I opted to use a key-value statement for my loop, ditching index, because of the very fact that knowing index is not necessary. It simply runs a scope per child of the ancestor. It’s easier on the eyes, doesn’t require unnecessary overhead (by a few ms anyway) or run into any potential trouble.