Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Help with deciding what script is better used?

Which is a better way of creating this script that anchors all parts in character?

FIRST SCRIPT:

game.Players.PlayerAdded:connect(function(Player)
	Player.CharacterAdded:connect(function(char)
		for i, v in pairs (char:GetChildren()) do
			if v:IsA("Part") then
				v.Anchored = true
			end
		end
	end)
end)]

SECOND SCRIPT:

game.Players.PlayerAdded:connect(function(Player)
	local Char = game.Workspace[Player.Name]
	for i, v in pairs (Char:GetChildren()) do
		if v:IsA("Part") then
			v.Anchored = true
		end
	end
end)

2 comments

0

You might want to use BasePart instead of Part.

0

What is difference?

1 answer

✓ Accepted answer

Clearly the first one...


The second one won't work

  • ...when the player's character takes time to load (hint: it always does)
  • ...when the player's character resets (hint: you can't disable the Reset button in the settings menu)

Even if you were using the second one, you should use player.Character, (and workspace not game.Workspace) .

If you only want it to happen the first spawn, you can use

local char = player.Character or player.CharacterAdded:wait()

which is a bit like connecting to the event, but only caring about the first one.

2 comments

0

"...when the player's character resets (hint: you can't disable the Reset button in the settings menu)" by changing the name of the character's humanoid the button breaks which technically disables it.

0

I need to remember that line of code. It can be very helpful, I think.

This discussion is preserved as it was. New answers, comments, and votes are closed.