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

Weld on player join?

So, I was messing around and trying to weld a part to my characters head when I joined, but it didn't seem to work and there were no errors.

game.Players.ChildAdded:connect(function(hit)
	local humanoid = hit:findFirstChild("Humanoid")
	if humanoid then
		local head = hit.Parent.Character.Head
		local torso = hit.Parent.Character.Torso
		script.Parent.CFrame = head.CFrame * CFrame.new(0,5,0)
		local weld = Instance.new("Weld")
		weld.Part0 = torso
		weld.C0 = torso.CFrame:inverse()
		weld.Part1 = script.Parent
		weld.C1 = script.Parent.CFrame:inverse()
		weld.Parent = script.Parent
		script.Parent.Anchored = false
	end
end)

2 answers

✓ Accepted answer

Your player and character are two difference things. When you do game.Players.ChildAdded you wait until an object is added in the Players service. When someone joins, their player is added to that service BUT their player is not their character. Things like Head/Torso/Humanoid/etc. exist in their Character so what you would want to do is run that when their Character is added.

local function characterAdded(character)
  -- do stuff with character (where Head/Torso/Humanoid should be, however you should make sure they exist first)
end

game.Players.PlayerAdded:connect(function(player)
  player.CharacterAdded:connect(characterAdded)
end)

1 comment

1

A good way to make sure they exist: use the WaitForChild method. For example local humanoid = character:WaitForChild("Humanoid")

here you go :D just place this script inside your spawn location /spawn point ->and then place your part inside a model called "Helmet" and place this Helmet model into your spawn point -get sure that your part is anchored

function onTouched(hit)
	if hit.Parent:findFirstChild("Humanoid") ~= nil and hit.Parent:findFirstChild("Helmet") == nil then
		local g = script.Parent.Parent.Helmet:clone()
		g.Parent = hit.Parent
		local C = g:GetChildren()
		for i=1, #C do
			if C[i].className == "Part" or C[i].className == "UnionOperation" or C[i].className == "WedgePart"  then
				local W = Instance.new("Weld")
				W.Part0 = g.Middle
				W.Part1 = C[i]
				local CJ = CFrame.new(g.Middle.Position)
				local C0 = g.Middle.CFrame:inverse()*CJ
				local C1 = C[i].CFrame:inverse()*CJ
				W.C0 = C0
				W.C1 = C1
				W.Parent = g.Middle
			end
				local Y = Instance.new("Weld")
				Y.Part0 = hit.Parent.Head
				Y.Part1 = g.Middle
				Y.C0 = CFrame.new(0, 0, 0)
				Y.Parent = Y.Part0
		end

		local h = g:GetChildren()
		for i = 1, # h do
			if h[i].className == "Part" or C[i].className == "UnionOperation" or C[i].className == "WedgePart"  then
				h[i].Anchored = false
				h[i].CanCollide = false
			end
		end
		
	end
end

script.Parent.Touched:connect(onTouched)

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