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!
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)