How to fix script only locking a few of the accessories?

Hey there, everyone!

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)
1 Like

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.

2 Likes

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)
1 Like

There is a high chance the player’s accessories won’t be loaded in time before the loop runs, which would achieve the same effect as the current code.

This would also lock the player’s accessories only when they join, and not every time they respawn.

2 Likes

Totally forgot about that, thanks for the correction ^^

2 Likes

Thanks for the solution! This worked for me.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.