How to make this code work

Alright! I am still a little confused on it, but hope it works!

It does not work, I’m not sure why.

I don’t need to check if it doesn’t exist, I need to check if it is the same attachment. If it is not, I can create the lineforce. If it is, then I should not.

If it is the same attachment, then what happens?

If it is a different attachment, then what happens?

If it’s the same, the code does not run

If it’s different, the code does run.

Not sure if this will work or not, but worth a try.

local Attachment = hit:FindFirstChild("Attachment")
local LineForce = p:FindFirstChild("LineForce")


if Attachment and LineForce and LineForce.Attachment1 ~= Attachment then
    LineForce:Destroy()
    line = Instance.new("LineForce", p)
	att0 = Instance.new("Attachment", p)
	att1 = Instance.new("Attachment", hit)
	att0.Name = "Attachment0"
	att1.Name = "Attachment1"
	line.InverseSquareLaw = true
	line.ReactionForceEnabled = true
	line.ApplyAtCenterOfMass = true
	line.Magnitude = p:GetMass()
	line.Attachment0 = att0
	line.Attachment1 = att1
end
1 Like
if not(hit:FindFirstChild("Attachment")) ==  p:FindFirstChild("LineForce"):FindFirstChild("Attachment1") then
	--uwu code
end

This wouldn’t work because I need to create the lineforces and attachments after p has been touched. Then, I need to set the attachments to the corresponding properties in the lineforces.

Attachment1 is a property of a LineForce, not a child. The attachment itself is inside p, so this wouldn’t work.

u can mentioned that before
uuh wait property or attribute

if not(hit:FindFirstChild("Attachment")) ==  p:FindFirstChild("LineForce"):GetAttribute("Attachment1") then
	--uwu code
end

Wait so you can’t put the code inside of the p touched event?

devforumthingy

No, it has to be inside the if statement, which is inside the function.

Unless someone can find a more efficient way to do this.

God it’s been a while since I opened the roblox studio ._.

if not(hit:FindFirstChild("Attachment")) and p:FindFirstChild("LineForce").Attachment1 == nil then
	--uwu code
end

This is only checking if the lineforce’s attachment1 is nil… Not if it is the same attachment inside of hit

Actually, this might work

Nevermind, because there could be multiple ps touching hit, so it does not know which attachment to choose-
Wait… This was also an issue in my original code… I know how to fix it.

Nevermind, I don’t know how to fix it. It still gives the same error, attempt to index nil with attachment1
@ianplus

Would you mind trying this code out?

if (not hit:FindFirstChild("Attachment")) ==  (p:FindFirstChild("LineForce") and p:FindFirstChild("LineForce"):FindFirstChild("Attachment1")) then
	-- blah.
end

If you’re trying to let the code inside of the if statement run under conditions of “Attachment” not being found under hit:FindFirstChild("Attachment") and “LineForsce” & “Attachment1” not being found under p:FindFirstChild("LineForce").Attachment1, you might want to try this code instead:

if not hit:FindFirstChild("Attachment") and not p:FindFirstChild("LineForce") and not p.LineForce:FindFirstChild("Attachment1") then
	-- blah.
end

Again, the attachment is NOT a child, it is a property. Just like PrimaryPart, it is a property attached to an object.

what type object is hit for it to have attachment?

Anyways if I am understanding correctly you need to compare the attachment1 property to the attachment1 property of lineforce also checking if lineforce is even present this should be how you need to write it or something similar

if hit:IsA('Beam') then    -- change beam to what ever class type it should be to have an attachement
	local LineForce = p:FindFirstChild("LineForce")  -- get lineforce if there
	
	if LineForce then   -- check if there is a lineforce 
		if hit.Attachement1 == LineForce.Attachment1 then  -- using attachement1 property if its attachment2 then change it here
			  -- attachment is set and the same
		else
			  -- not set or not the same
		end
	else
		print('No LineForce')  -- just idicate no line force found you can remove this
	end
end

This would work, it’s just that the code does not point to a specific attachment of a specific line force. This is an issue with my original code too which I noticed a bit of time ago, and I’m not sure how to fix it.