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

Making player body parts anchored upon joining?

Here's the current script.

game.Players.PlayerAdded:connect(function(player)
	player:WaitForChild("Character")
		local bodyParts = {
		player.Character.Head;
		player.Character.Torso;
		player.Character:FindFirstChild("Left Arm");
		player.Character:FindFirstChild("Right Arm");
		player.Character:FindFirstChild("Left Leg");
		player.Character:FindFirstChild("Right Leg");
		}
	bodyParts.Anchored = true
end)

It waits until the player joins the game, then attempts waiting for the "Character" child of the player. Once that's settled it makes a table of the body parts that the player has and changes them to be anchored afterward, however, the script doesn't seem to be interfering with the body parts. The error is probably at line two.

1 comment

0

You don't need to get the body parts you just need to get the character of the player and use this script --R15 Character.HumanoidRootPart.Anchored = true -- R6 Character.Torso.Anchored = true

3 answers

✓ Accepted answer

Here's a fixed version of your script:

game.Players.PlayerAdded:connect(function(player)
    player.CharacterAdded:wait()
        local bodyParts = {
	    player.Character.Head;
	    player.Character.Torso;
	    player.Character:FindFirstChild("Left Arm");
	    player.Character:FindFirstChild("Right Arm");
	    player.Character:FindFirstChild("Left Leg");
	    player.Character:FindFirstChild("Right Leg");
        }
    
    for _, part in pairs(bodyParts) do
        part.Anchored = true
    end
end)

There were two main issues with your script.

First, as you suspected, this line:

player:WaitForChild("Character")

The issue with this is that WaitForChild waits for a child of an instance. However, Character is a property of Player, not a child.

Instead of that, we can hook into the CharacterAdded event to wait until the character is created like so:

player.CharacterAdded:wait()

You also had this line of code:

bodyParts.Anchored = true

Which will not work. The reason this will not work is because bodyParts is a table, not a ROBLOX object.

Instead, we do this:

for _, part in pairs(bodyParts) do
    part.Anchored = true
end

To iterate through all of the parts in the bodyParts table and anchor them.

I hope this helps! Good luck.

1 comment

0

Thank you , you've been really helpful today.

Your issue is that :WaitForChild() waits for an instance, not a property. Character is a property of player which references the model for which the player controls. Here's a better way to handle this each time they **respawn **or spawn.

You also have a second issue. You are setting the property of a table "Anchored" and not the parts. You need to access each part.

game.Players.PlayerAdded:connect(function(player)
	player.CharacterAdded:connect(function(Character)
		local BodyParts = {
			Character:WaitForChild("Head")
			Character:WaitForChild("Torso")
			Character:WaitForChild("Left Arm")
			Character:WaitForChild("Left Leg")
			Character:WaitForChild("Right Arm")
			Character:WaitForChild("Right Leg")
		}
		for i, Part in BodyParts do
			Part.Anchored = true
		end
	end)
end)

Well, ROBLOX's :WaitForChild function seems to not work most of the time. I prefer using this function I made:

function WaitForChild(Parent, ChildName)
	local try = 0
	repeat
		if Parent:FindFirstChild(ChildName) or try >= 50 then 
			break --Ensure that the loop won't continue on even if it finds the child or it can't find it after 5 seconds
		end
		wait(0.1)
		try = try+1
	until Parent:FindFirstChild(ChildName)
	return Parent:FindFirstChild(ChildName) --If it can't find it, it'll just return nil.
end

2 comments

0

WaitForChild always works. This function is a delayed alternative to WaitForChild. Moreover, it's not as efficient, as WaitForChild hooks into child added events.

0

Oh, well, for me, WaitForChild almost never works...

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