Beam is not attaching on attachments

		local part = Instance.new("Part")
		part.Name = "Point"
		part.Anchored = true
		part.Position = pos
		part.Size = Vector3.new(2,2,2)
		part.Transparency = 1
		part.CanCollide = false
		part.Parent = workspace
		
		local At1 = Instance.new("Attachment")
		At1.Name = "Attachment1"
		At1.Position = Vector3.new(0,0,0)
		At1.WorldPosition = part.Position
		At1.Parent = part
		
		local At0 = Instance.new("Attachment")
		At0.Name = "Attachment0"
		At0.Position = Vector3.new(0,0,0)
		At0.WorldPosition = leftG.Position
		At0.Parent = leftG
		
		local beam = Instance.new("Beam")
		beam.FaceCamera = true;
		beam.Color = ColorSequence.new({ColorSequenceKeypoint.new(0,Color3.new(0,0,13/51)),ColorSequenceKeypoint.new(1,Color3.new(0,0,13/51))});
		beam.ZOffset = 0;
		beam.Transparency = NumberSequence.new({NumberSequenceKeypoint.new(0,0,0),NumberSequenceKeypoint.new(1,0,0)});
		beam.LightInfluence = 1;
		beam.CurveSize0 = 1;
		beam.Segments = 1;
		beam.Attachment0 = At0;
		beam.Texture = "rbxassetid://4768994854";
		beam.CurveSize1 = 1;
		beam.TextureMode = Enum.TextureMode.Static;
		beam.Attachment1 = At1;
		beam.Parent = part

A part is instaced and Attachment1 is childed to the part and another attachment, attachment0 is childed to LeftG(Another Part) and in the end the beam gets connected to both the attachments

I want to make the beam attach to attachments but it’s not working even showing.

Note: I am using a local script to do this on the client.

Mhm that’s weird, I’ve tested your script in Studio and it worked perfectly.

Is your variable pos a Vector3 and where is your LocalScript located ?

1 Like

Yes, pos is a cFrame value from mouse.Hit.p.
My Local Script is located inside “StarterPlayerScripts”

When I play it the beam gets randomly positioned in the sky and disappears

Alright I think I have found the issue:
Use .CFrame instead of .Position

So, it should look like this:
part.CFrame = pos

1 Like

I did that however its the same and the part gets instanced and positioned wherever my mouse is.

Can you provide me the script that send mouse.Hit.p to this function please ?

1 Like

Yeah sure, here;

UI.InputBegan:Connect(function(input, gp)
	if input.KeyCode == Enum.KeyCode.Q then
		local char = plr.Character 
		local pos = mouse.hit.p
		local getPart = mouse.Target
		


		if getPart then

		local part = Instance.new("Part")
		part.Name = "Point"
		part.Anchored = true
		part.Position = pos
		part.Size = Vector3.new(2,2,2)
		part.Transparency = 1
		part.CanCollide = false
		part.Parent = workspace
		
		local At1 = Instance.new("Attachment")
		At1.Name = "Attachment1"
		At1.Position = Vector3.new(0,0,0)
		At1.WorldPosition = part.Position
		At1.Parent = part
		
		local At0 = Instance.new("Attachment")
		At0.Name = "Attachment0"
		At0.Position = Vector3.new(0,0,0)
		At0.WorldPosition = leftG.Position
		At0.Parent = leftG
		
		local beam = Instance.new("Beam")
		beam.FaceCamera = true;
		beam.Color = ColorSequence.new({ColorSequenceKeypoint.new(0,Color3.new(0,0,13/51)),ColorSequenceKeypoint.new(1,Color3.new(0,0,13/51))});
		beam.ZOffset = 0;
		beam.Transparency = NumberSequence.new({NumberSequenceKeypoint.new(0,0,0),NumberSequenceKeypoint.new(1,0,0)});
		beam.LightInfluence = 1;
		beam.CurveSize0 = 1;
		beam.Segments = 1;
		beam.Attachment0 = At0;
		beam.Texture = "rbxassetid://4768994854";
		beam.CurveSize1 = 1;
		beam.TextureMode = Enum.TextureMode.Static;
		beam.Attachment1 = At1;
		beam.Parent = part

end
end)

Alright, I’ve tested it in studio and the beam was connecting perfectly with the objects and their attachments

Here’s a quick video I took reacting to my script:

As you can see in the video, everytime I press Q it creates a new part and add a Beam attached to it.


Here’s the LocalScript (located in StarterPlayerScripts):

local UI = game:GetService("UserInputService")
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()

UI.InputBegan:Connect(function(input, gp)
	if input.KeyCode == Enum.KeyCode.Q then
		
		local char = plr.Character 
		local pos = mouse.hit.p
		local getPart = mouse.Target

		if getPart then

			local part = Instance.new("Part")
			part.Name = "Point"
			part.Anchored = true
			part.CFrame = CFrame.new(-38.9, 8.9, -12) -- change to pos
			part.Size = Vector3.new(2,2,2)
			part.Transparency = 0.5 -- 1
			part.CanCollide = false
			part.Parent = workspace
			
			local At1 = Instance.new("Attachment")
			At1.Name = "Attachment1"
			At1.WorldPosition = part.Position
			At1.Parent = part
			At1.Position = Vector3.new(0,0,0)
			
			local At0 = Instance.new("Attachment")
			At0.Name = "Attachment0"
			At0.WorldPosition = workspace:WaitForChild('Part').Position -- change to LeftG
			At0.Parent = workspace:WaitForChild('Part') -- change to LeftG
			At0.Position = Vector3.new(0,0,0)
			
			local beam = Instance.new("Beam")
			beam.FaceCamera = true;
			beam.Color = ColorSequence.new({ColorSequenceKeypoint.new(0,Color3.new(0,0,13/51)),ColorSequenceKeypoint.new(1,Color3.new(0,0,13/51))});
			beam.ZOffset = 0;
			beam.Transparency = NumberSequence.new({NumberSequenceKeypoint.new(0,0,0),NumberSequenceKeypoint.new(1,0,0)});
			beam.LightInfluence = 1;
			beam.CurveSize0 = 1;
			beam.Segments = 1;
			beam.Attachment0 = At0;
			beam.Texture = "rbxassetid://4768994854";
			beam.CurveSize1 = 1;
			beam.TextureMode = Enum.TextureMode.Static;
			beam.Attachment1 = At1;
			beam.Parent = part
		end
	end
end)

Oh thanks for telling me that, can you try to this same thing but with mouse.hit.p?


I clicked somewhere else and my attachment is positioned somewhere else

Of course, because the way you made the script is:

Everytime you press Q, it takes the player’s mouse location and creates a part depending on the location. Could you tell me what exactly do you want to do with that script please or what it’s supposed to do?

1 Like

I want the script to position the attachment wherever I click.

So I made it like a part is instanced with an attachment inside it.

But

When I click on the tower the attachment is of the other end of the beam is positioned outside the baseplate just like in the screenshot above

Something like this ?
image

yes exactly. But my attachments are acting weird and not positioning where my mouse is :slightly_frowning_face:

Here’s what I came up with:

local plr = game.Players.LocalPlayer
local char = plr.Character

local mouse = plr:GetMouse()
mouse.TargetFilter = workspace.DONTTOUCH -- create a model called 'DONTTOUCH' in the workspace (used so when you hit the mouse, the attachment filters properly so it doesn't get positioned out of the tower)


local towersfolder =  workspace.Towers
local clicking = false

-- create beam & attachment in character (only one time)
local function createBeam()
	
	if not char or not char.Parent then
	    char = plr.CharacterAdded:wait()
	end

	wait(2)
	local At0 = Instance.new("Attachment")
	At0.Name = "Attachment0"
	At0.WorldPosition = char.HumanoidRootPart.Position -- to change if needed
	At0.Parent = char.HumanoidRootPart -- to change if needed
	At0.Position = Vector3.new(0,0,0)
	
	local beam = Instance.new("Beam")
	beam.FaceCamera = true;
	beam.Color = ColorSequence.new({ColorSequenceKeypoint.new(0,Color3.new(0,0,13/51)),ColorSequenceKeypoint.new(1,Color3.new(0,0,13/51))});
	beam.ZOffset = 0;
	beam.Transparency = NumberSequence.new({NumberSequenceKeypoint.new(0,0,0),NumberSequenceKeypoint.new(1,0,0)});
	beam.LightInfluence = 1;
	beam.CurveSize0 = 1;
	beam.Segments = 1;
	beam.Attachment0 = At0;
	beam.Texture = "rbxassetid://4768994854";
	beam.CurveSize1 = 1;
	beam.TextureMode = Enum.TextureMode.Static;
	beam.Parent = char
	beam.Enabled = false
end

createBeam()
mouse.Button1Down:connect(function() -- when mouse is clicked
	
	if not char or not char.Parent then
    	char = plr.CharacterAdded:wait()
	end
	if clicking then
		return
	end
	if mouse.Target then
		if mouse.Target.Parent.Name == 'Towers' then -- create folder, insert all your towers in it, and change the value by the folder's name

			clicking = true
			local tower = mouse.Target
			if tower:FindFirstChild('Attachment1') then
				tower.Attachment1:Destroy() -- destroy the attachment if it exists
			end

			-- creates a new attachment
			local At1 = Instance.new("Attachment")
			At1.Name = "Attachment1"
			At1.WorldPosition = tower.Position
			At1.Parent = tower
			At1.Position = Vector3.new(0, mouse.Origin.p.Y, 0)
		
			-- check if the attachment is in the tower
			if mouse.Hit.p.X >= tower.Position.X and mouse.Hit.p.Z >= tower.Position.Z then
				At1.Position = Vector3.new(0, mouse.Hit.p.Y, mouse.Hit.p.Z)
			end
			if At1.Position.Y > tower.Position.Y then
				At1.Position = Vector3.new(At1.Position.X, tower.Position.Y, At1.Position.Z)	
			end
			
			-- activates the beam
			char.Beam.Attachment1 = At1
			At1.Visible = true
			char.Beam.Enabled = true
		end
	end
end)
mouse.Button1Up:connect(function() -- when mouse is up
	clicking = false
end)

I roughly made that in an hour, the script works though. However, it will probably need some adjustments for the mouse position, but here’s a quick video:

If you have any questions, hit me up! :slight_smile:

2 Likes

yes thank you!

I tried this and adjusting it a bit, but like in the video you posted there is a big offset when I click on parts.
The attachments are still flinging

I think I am still doing something wrong with the positioning of the attachments

I fixed it by declaring the Worldposition after attaching beam attachments.
Phew I feel so relieved now

1 Like