How do I rotate and move this beam to make it come out of my gun?

I am making a laser gun, but the projectile that it shoots does not actually go in the correct direction.

Here is my script.

 -- Variables --
    local Folder = game.Workspace.Projectiles
    local Debris = game:GetService("Debris")
    local Player = game.Players.PlayerAdded:Wait()
    local Character = Player.CharacterAdded:Wait()
    -- Main --

    local function CreateBeam(Origin, Direction, Rotation)
    	local MidPoint = Origin + Direction/2
    	
    	local Beam = game.ReplicatedStorage.Beam:Clone()
    	Beam.Parent = Folder
    	Beam.Anchored = true
    	Beam.CanCollide = false
    	
    	
    	Beam.CFrame = CFrame.new(MidPoint, Origin)
    	Beam.Size = Vector3.new(.5,.5, Direction.magnitude)
    	
    	
    	Debris:AddItem(Beam, 2)
    end

    game.ReplicatedStorage.Events.GunFireEvent.OnServerEvent:Connect(function(Player, MousePos, OriginPos)
    	local Direction = Character.Pistol.Handle.CFrame.LookVector
    	local Rotation = Character.Pistol.Handle.Origin.WorldPosition
    	
    	local RayCast = workspace:Raycast(OriginPos, Direction)
    	
    	if RayCast then 
    		if RayCast.Instance.Name == "Head" then
    			local Character = RayCast.Instance.Parent
    			local Humanoid = Character:FindFirstChild("Humanoid")
    			
    			if Humanoid then
    				Humanoid:TakeDamage(95)
    			end
    		else
    			local Character = RayCast.Instance.Parent
    			local Humanoid = Character:FindFirstChild("Humanoid")
    			
    			if Humanoid then
    				Humanoid:TakeDamage(35)
    			end	
    		end
    		
    		if RayCast.Instance.Parent.Name == "Helmet" then
    			local Character = RayCast.Instance.Parent.Parent
    			local Humanoid = Character:FindFirstChild("Humanoid")

    			if Humanoid then
    				Humanoid:TakeDamage(15)
    			end	
    		end
    		
    		if RayCast.Instance.Parent.Name == "FLC" then
    			local Character = RayCast.Instance.Parent.Parent
    			local Humanoid = Character:FindFirstChild("Humanoid")

    			if Humanoid then
    				Humanoid:TakeDamage(5)
    			end	
    		end
    	end
    	
    	game.ReplicatedStorage.Events.PistolSoundEvent:FireAllClients()
    	CreateBeam(OriginPos, Direction, Rotation)
    end)

Is there a way I can fix this?

local Direction = Character.Pistol.Handle.CFrame.LookVector
should be
local Direction = MousePos - OriginPos

That fixed the rotation, but the beam doesn’t look like its coming out of the gun.

Try
local Direction = MousePos - Character.Pistol.Handle.Origin.WorldPosition

Now the beam doesn’t follow the mouse at all, its just going in random directions and sometimes hurts the player

-- Variables --

local Tool = script.Parent

local Player = game.Players.LocalPlayer

local Mouse = Player:GetMouse()

local Origin = Tool.Handle.Origin

local Projectiles = game.Workspace.Projectiles

-- Functions --

local function Shoot()

    local MousePos = Mouse.Hit.p

    local OriginPos = Origin.WorldPosition

    game.ReplicatedStorage.Events.GunFireEvent:FireServer(MousePos, OriginPos)

end

-- Connect --

Tool.Activated:Connect(Shoot) 

Here is the client script.

Try replace
local Rotation = Character.Pistol.Handle.Origin.WorldPosition
with
local OriginPos = Character.Pistol.Handle.Origin.WorldPosition

That is what OriginPos is, it’s the attachment’s world position.

yes but due to lag between server and client you might aswell just get it on the server side.


I don’t think its client and server lag, it still does the same exact thing.

Could it be something to do with the raycast?

local function CreateBeam(Origin, MousePos)

	local Beam = game.ReplicatedStorage.Beam:Clone()
	Beam.Parent = Folder
	Beam.Anchored = true
	Beam.CanCollide = false
	
	local Distance = (Origin - MousePos).Magnitude
	Beam.CFrame = CFrame.new(Origin, MousePos) * CFrame.new(0, 0, -Distance / 2)
	Beam.Size = Vector3.new(0.5, 0.5, Distance)

	Debris:AddItem(Beam, 2)
end

CreateBeam(OriginPos, MousePos)