I’m trying to lock a user’s body-parts and accessories upon they join the game, but for some reason, only a few of the accessories get locked. How should I fix this problem?
This is the LocalScript that I have placed in StarterPlayerScripts:
game.Players.PlayerAdded:Connect(function(player)
local character = player.Character
if character then
local bodyParts = character:GetDescendants()
for _, part in ipairs(bodyParts) do
if part:IsA("BasePart") then
part.Locked = true
end
end
local accessories = character:FindFirstChild("Humanoid"):GetAccessories()
for _, accessory in ipairs(accessories) do
accessory.Handle.Locked = true
end
end
end)
Just tried it myself and this seemed to be the solution for me: All you have to do is change
local character = player.Character
to:
local character = player.Character or player.CharacterAdded:Wait()
And make sure it’s a server script and not a local script. You also won’t need the second loop for the accessories as you’re already affecting every descendant basepart in the first loop.
Remove your LocalScript in StarterPlayerScripts and add a new Script in ServerScriptService.
You need to wait for .CharacterAppearanceLoaded, and then the accessories will be loaded in.
Code:
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
player.CharacterAppearanceLoaded:Connect(function(character)
for i, descendant in character:GetDescendants() do
if descendant:IsA("BasePart") then
descendant.Locked = true
end
end
end)
end)