Laser not appearing at expected coordinates

I’m currently attempting to understand how to create a laser from an object to a humanoid…

With the script that I have thus far, the laser beams do not appear where I’m expecting them to (from the sphere to the player)… thinking that perhaps I’m misunderstanding something here (Note that I’m aware there will be a bunch of attachments created and not destroyed with the script as-is… needed to see where the beams were appearing)…

local laser = script.Parent
local laserInterval = 5
local laserDamage = 5
local laserDistance = 100


while wait(laserInterval) do
	local laserTarget = nil
	local laserOrigin = nil
	local laserDestination = nil
	
	print("looking for target...")
	
	for i, v in pairs (game.Workspace:GetChildren()) do
		print("think I found something")
		local humanoid = v:FindFirstChild("Humanoid")
		local torso = v:FindFirstChild("UpperTorso")
		
		if humanoid and torso and humanoid.Health > 0 then
			if (torso.Position - laser.Position).magnitude < laserDistance then
				print("found a live one!")
				laserTarget = torso
				laserOrigin = laser.Position
				laserDestination = laserTarget.Position
				print("target is: ",laserTarget)
			end
			if laserTarget then
				print("working on laser beam...")
				print("target is: ",laserTarget)
				-- Create the attachments for the beam and parent to laser
				local att0 = Instance.new("Attachment")
				local att1 = Instance.new("Attachment")
				att0.Parent = laser
				att1.Parent = laser
				
				-- Position attachments
				print("laserOrigin: ",laserOrigin)
				print("laserDestination: ",laserDestination)
				att0.Position = Vector3.new(laserOrigin.X,laserOrigin.Y,laserOrigin.Z)
				att1.Position = Vector3.new(laserDestination.X,laserDestination.Y,laserDestination.Z)
				print("att0 position: ",att0.Position)
				print("att1 position: ",att1.Position)	
				att0.Visible = true
				
				-- Create the beam
				local laserBeam = Instance.new("Beam")
				laserBeam.Attachment0 = att0
				laserBeam.Attachment1 = att1
				print("attachment0 position: ",laserBeam.Attachment0.Position)
				
				-- Beam properties
				laserBeam.FaceCamera = true
				local colorOne = Color3.new(1,0,0)
				local colorTwo = Color3.new(1,0,0)
				local colorSequence = ColorSequence.new(colorOne,colorTwo)
				laserBeam.Color = colorSequence
				laserBeam.FaceCamera = true
				laserBeam.Width0 = 0.5
				laserBeam.Width1 = 1
				
				-- Parent beam to att0
				laserBeam.Enabled = true			
				laserBeam.Parent = att0
			end	
		end
	end
end

Position in this case is a relative displacement from the center of the object that the attachments are parented to. Just change Position to WorldPosition.

Excellent, makes total sense… Works as expected… thx!

1 Like