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

welding part to head without touched event?

I want the Part to weld to the Character's Head when the player spawns into the game. But, welding don't have the PlayerAdded event. How would I go about this?

function JustWelding (hit)
	
	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	
		if humanoid then
			local head = hit.Parent.Head
			local torso = hit.Parent.Torso
			script.Parent.CFrame = head.CFrame * CFrame.new(0,0,5)
			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

script.Parent.Touched:connect(JustWelding)

It works great with the touched event! Now I need it to do it when the character spawns into the game.

Help would be appreciated!

1 answer

✓ Accepted answer

For this, it would be better to use the CharacterAdded event rather than PlayerAdded, as the former will also run after the player respawns. You will need the PlayerAdded to connect a function to CharacterAdded, however. You will need to use the Character property of Player to get the player's character and use that for the function instead.

function JustWelding (character)
	local head = character.Head
	local torso = character.Torso
	script.Parent.CFrame = head.CFrame * CFrame.new(0,0,5)
	local weld = Instance.new("Weld")
	weld.Part0 = torso
	weld.C0 = torso.CFrame:inverse()
	weld.Part1 = script.Parent
	weld.C1 = script.Parent.CFrame:inverse()
	wait() -- This seems to fix the problem.
	weld.Parent = script.Parent
	script.Parent.Anchored = false
end

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

3 comments

0

Nothing happen.

0

Fixed the problem. Not sure why this works; I haven't used welds in a while.

0

Thank you.

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