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

How to make a rope constraint between two objects?

This script is supposed to make a rope between the person who shoots the projectile, and the body part of whoever it hits. But it keeps telling me " attempt to index field 'Attachment0' (a nil value)"

Character = script.Parent.Parent.Parent

function touch(hit)
local	humanoid = hit.Parent:FindFirstChild("Humanoid")
    if humanoid and humanoid.Parent.Name ~= script.Parent.Parent.Parent.Name  then
local wire = Instance.new("RopeConstraint")
wire.Parent = Character.UpperTorso
wire.Attachment0.Parent = Character.UpperTorso
wire.Attachment1.Parent = hit
wire.Length = 100

end

end



script.Parent.Touched:connect(touch)

2 comments

0

Thanks. But now it says "bad cast".

0

Yeah make sure you are attaching them to actual attachments not the base part itself

2 answers

✓ Accepted answer

That's because Attachment0 and Attachment1 have to be set before you do anything with them. This should work for what you're doing

function touch(hit)
	local   humanoid = hit.Parent:FindFirstChild("Humanoid")
	if humanoid and humanoid.Parent.Name ~= script.Parent.Parent.Parent.Name  then
		local wire = Instance.new("RopeConstraint")
		wire.Attachment0 = Instance.new("Attachment", Character.UpperTorso)
		wire.Attachment1 = Instance.new("Attachment", hit)
		wire.Length = 100
		wire.Parent = Character.UpperTorso
	end
end

5 comments

0

Ah, thanks bro!

0

The parent argument for instance.new is deprecated, and you did not check if there is an attachment already. This will spam attachments inside the character, as well as on the touching part.

0

Might want to fix it so the attachments don't spam.

0

Hey, how do you make the length of the constraint equal to the distance between the two attachments? Thanks.

1

^ wire.Length = (char.UpperTorso.Position - hit.Position).Magnitude

###This is because you didn't provide any Attachments. You need two Attachment objects.

local char = script.Parent.Parent.Parent
local upper = char.UpperTorso

local touch

touch = function(part)
    local humanoid = part.Parent:FindFirstChild("Humanoid")

    if humanoid then
        local attachment0 = Instance.new("Attachment") or upper:FindFirstChild("RopeAttachment0") 

        attachment0.Name = "RopeAttachment0"
        attachment0.Parent = Character.UpperTorso

        local attachment1 = Instance.new("Attachment") or part:FindFirstChild("RopeAttachment1")
        attachment0.Name = "RopeAttachment1"
        attachment0.Parent = part

        wire.Attachment0 = attachment0
        wire.Attachment1 = attachment1
    end
end

script.Parent.Touched:Connect(touch) 
-- Use :Connect, :connect is deprecated

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