Help with Beam and head rotation

So I’m trying to make the beam position correctly with his eyes and make the head and the beam rotate where the camera is looking. I’m not really good a scripting so this is the code I have.

--||Service||--
local Runservice = game:GetService("RunService")
local UserInput = game:GetService("UserInputService")
local RS = game:GetService("ReplicatedStorage")
--||Variables||--
local Tool = script.Parent
local Beam = RS.BEAMPART
local Debounce = true
local flare1 = Tool.Handle.Flare1
local flare2 = Tool.Handle.Flare2

Tool.Equipped:Connect(function()

	flare1.Parent = Tool.Parent:FindFirstChild("Head")
	flare2.Parent = Tool.Parent:FindFirstChild("Head")

	flare1:Emit(5)
	flare2:Emit(5)
end)

Tool.Activated:Connect(function()

	if Debounce then

	Debounce = false

	print(Tool.Parent.Name)

	Beam.Parent = workspace
	Beam.Position = Tool.Parent["Head"].Position + CFrame.new(0,0,15)
	Beam:Clone()

	task.wait(1)

	Debounce = true
	end
end)

if not Debounce then
	Runservice.RenderStepped:Connect(function()
	local head = Tool.Parent:FindFirstChild("Head")
		local camera = workspace.CurrentCamera
		head.Position = camera.CFrame:toObjectSpace(camera.CFrame).lookVector


	end)

end

You cannot add a cframe to a vector; you can, however, add a vector to a cframe (I think).

So if we go off of the picture you gave, we can do something like this:

local head = char.Head or char:WaitForChild("Head")

local beam = "beam_here"

local beam_length = 5

local dir = head.CFrame.LookVector
local pos = head.CFrame.p

local cf = CFrame.new(pos, dir)

beam.Position = (cf * CFrame.new(0, 0, -beam_length)).p

-- the "p" is something you can do with CFrame's and is just a shorter way of typing CFrame.Position

-- this is just a theory I haven't tested this in studio so it might not work.
3 Likes

Do you mean

local dir = head.CFrame.LookVector
local pos = head.CFrame.p

local cf = CFrame.lookAlong(pos, dir)

And don’t forget to divide beam_length by 2
Other than that it looks interesting. Another problem is you would probably want the rotation as well as the position right?

I have no clue what lookAlong does, but if you look at CFrame.new(), you can either do CFrame.new(x, y, z) – positions or CFrame.new(pos, dir) – pos vector3 as the first value and dir vector3 as the second value. And no, you won’t divide beam_length by 2 or anything like that.

so there’s no need for run service with this?

or maybe this is were i put it there

This was mainly just an example, but to answer your question, if you were trying to make it point in the direction of the head once, then no, but if you were trying to make it point in the direction of the head at all times, then yes, you would need one, and this would be the only part you need in the loop:

local dir = head.CFrame.LookVector
local pos = head.CFrame.p

local cf = CFrame.new(pos, dir)

beam.Position = (cf * CFrame.new(0, 0, -beam_length)).p

I think you should just create a Weld inside BEAMPART. Then just change the weld property Part0 to head and Part1 to BEAMPART via script inside the Activated function. Then you can just edit the C0 or C1 property of the weld (in studio) to get it to how you want it.

It’s CFrame.new(pos, pos+dir)
or you can do
CFrame.lookAlong(pos, dir)

respectfully it is not.

(chars)

Edit: I was proven wrong.


lookAlong ^




new ^


new with pos + dir

1 Like

Well then, I’ve never seen this happen, but you proved me wrong. Good job.


So this is the weld idea I had. You can create one inside the beampart and then create a rig and set Part1 to the BEAMPART and Part0 to the Rig’s Head. This is so you can change the C0 property until it looks something like this or however you want it. Then just set Part1 and Part0 to nil, or simply delete the rig you created. Now all you would need to do then is set Part1 and Part0 of the weld when the beam is activated.


yeah?

this is what i have rn…

--||Service||--
local Runservice = game:GetService("RunService")
local UserInput = game:GetService("UserInputService")
local RS = game:GetService("ReplicatedStorage")
--||Variables||--
local Tool = script.Parent
local char = Tool.Parent
local Beam = RS.BEAMPART
local Debounce = true
local flare1 = Tool.Handle.Flare1
local flare2 = Tool.Handle.Flare2

Tool.Equipped:Connect(function()

	flare1.Parent = Tool.Parent:FindFirstChild("Head")
	flare2.Parent = Tool.Parent:FindFirstChild("Head")

	flare1:Emit(5)
	flare2:Emit(5)
end)

Tool.Activated:Connect(function()

	if Debounce then

		Debounce = false

		print(Tool.Parent.Name)

		Beam.Parent = workspace
		Beam:Clone()
		Beam.Weld.C0 = char:WaitForChild("Head")
		Beam.Weld.C1 = Beam

		task.wait(1)

		Beam.Weld.C0 = nil
		Beam.Weld.C1 = nil

		Debounce = true



	end
end)

if Debounce then

	
	local head = char.Head or char:WaitForChild("Head")

	local beam = "beam_here"

	local beam_length = 5

	local dir = head.CFrame.LookVector
	local pos = head.CFrame.p

	local cf = CFrame.lookAlong(pos, dir)

	Beam.Position = (cf * CFrame.new(0, 0, -beam_length)).p


	end

and no the head rotation script isnt working

instead do

local clonedBeam = Beam:Clone()
clonedBeam.Parent = workspace
clonedBeam.Weld.Part0 = char:WaitForChild("Head")
clonedBeam.Weld.Part1 = Beam

task.wait(1)

clonedBeam:Destroy()

i also cant get it from spawning here

wdww

I think the only consistent way to keep it from spawning there is by keeping the BEAMPART outside of workspace. Like in ReplicatedStorage, for example.

thats what i didddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd

--||Service||--
local Runservice = game:GetService("RunService")
local UserInput = game:GetService("UserInputService")
local RS = game:GetService("ReplicatedStorage")
--||Variables||--
local Tool = script.Parent
local char = Tool.Parent
local Beam = RS.BEAMPART
local Debounce = true
local flare1 = Tool.Handle.Flare1
local flare2 = Tool.Handle.Flare2

Tool.Equipped:Connect(function()

	flare1.Parent = Tool.Parent:FindFirstChild("Head")
	flare2.Parent = Tool.Parent:FindFirstChild("Head")

	flare1:Emit(5)
	flare2:Emit(5)
end)

Tool.Activated:Connect(function()

	if Debounce then

		Debounce = false

		print(Tool.Parent.Name)

		local clonedBeam = Beam:Clone()
		clonedBeam.Parent = workspace
		clonedBeam.Weld.Part0 = char:FindFirstChild("Head")
		clonedBeam.Weld.Part1 = clonedBeam
		clonedBeam.Weld.C0 = CFrame.new(0, 0, 21) * CFrame.Angles(0, 90, 0)
		clonedBeam.Weld.C1 = CFrame.new(0,0,0)


		task.wait(1)

		clonedBeam:Destroy()

		Debounce = true

	end

end)
1 Like