Need to create weld between 2 players when they are touching

  1. What do you want to achieve?

I need to write a script which will attatch a player1 to a player2 if player2 touches player1’s hand. I have the general idea/pseudocode for how to code this, something along these lines:

(script is in player's hand)

if(script.parent is touching player2) do 
     create new weld between player1 and player2
  1. What is the issue?

I have legitimately no idea how to code what’s in the if condition (script.parent touching player2)

  1. What have you tried before?
    I searched on devforum for “players touching” but got nothing.

Sorry if this seems trivial, I’m fairly new to scripting. Any feedback would help.

For touching, you would use the .Touched event.

(Note that connecting two players together can create many issues due to ping differences, so use at caution. To minimize this, I am connecting the two players by RopeConstraint)

For example, if you are using R6, and you want the right hand to touch the left hand of another player, you can do

local players = game:GetService("Players") --This service is the Players service, basically it handles things involving players

local character = --This is where you put the character that will do the touching

local holding = false --This depicts whether the player is holding hands or not

character["Right Arm"].Touched:Connect(function(hit) --The reason the right arm is formatted like this is because there is a space in the naming
     if hit.Parent ~= character and hit.Name == "Left Arm" and players:GetPlayerFromCharacter(hit.Parent) ~= nil and holding == false then
        local attachment1 = Instance.new("Attachment")
        local attachment2 = Instance.new("Attachment")
        attachment1.Parent = character["Right Arm"]
        attachment2.Parent = hit
        attachment1.CFrame = CFrame.new(0, -1, 0)
        attachment2.CFrame = CFrame.new(0, -1, 0)
        local rope = Instance.new("RopeConstraint")
        rope.Parent = game.Workspace
        rope.Length = 3
        rope.Attachment0 = attachment1
        rope.Attachment1 = attachment2
        holding = true
    end
end)
1 Like

Thank you so much for the help!

Quick question: If I put my script in StarterPlayerScripts, will script.Parent refer to the player?

Sorry for the late response. If you were to put it into StarterPlayerScripts, that script will be parented under the player (not the character). If you want it to be parented under the character, you will put it into StarterCharacterScripts. So yes, it will refer to the player if you do put it under StarterPlayerScripts.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.