Need help making a beam follow the direction of the user's mouse

try this

-- Server Script
local wand = script.Parent
local handle = wand.Handle
local replicatedStorage = game:GetService('ReplicatedStorage')
local remoteEvent = replicatedStorage.Wand.RemoteEvent
local equipped = false
local beamMode = true
local charging = false
local mousePosition

-- When wand is equipped
wand.Equipped:Connect(function()
    equipped = true
end)

-- When wand is unequipped
wand.Unequipped:Connect(function()
    charging = false
    equipped = false
end)

-- Retrieves position of mouse from client
remoteEvent.OnServerEvent:Connect(function(player, mousePos)
    mousePosition = mousePos
end)

-- When wand is clicked with left click
wand.Activated:Connect(function()
    local magic = wand:FindFirstChild('Magic')
    if not magic then
        magic = wand.MagicToClone:Clone()
        magic.Name = 'Magic'
        magic.Parent = wand
        magic.Start.Anchored = false
        magic.Start.Position = wand.Point.Position

        local weldConstraint = Instance.new('WeldConstraint')
        weldConstraint.Parent = handle
        weldConstraint.Part0 = handle
        print(magic.Start)
        weldConstraint.Part1 = magic.Start
    end

    if equipped then
        if beamMode then
            if not charging then
                charging = true
                print('Charging...')
                for _, object in magic.Start:GetChildren() do
                    if object:IsA('ParticleEmitter') then
                        object.Transparency = NumberSequence.new(0)
                        -- Charging animation
                    end
                end
            else
                charging = false
                print('Released!')
                -- Release Animation

                for _, object in magic:GetChildren() do
                    for _, obj in object:GetChildren() do
                        if obj:IsA('Beam') or obj:IsA('ParticleEmitter') then
                            obj.Transparency = NumberSequence.new(0)
                        end
                    end
                end

                magic.End.Position = magic.Start.Position

                local amount = 1000
                local direction = (mousePosition.Position - magic.Start.Position).Unit
                while (pcall(addPosition, magic, direction, amount)) do
                    addPosition(magic, direction, amount)
                    task.wait(1)
                end
            end
        else
            print('not beammode')
        end
    end
end)

-- When mouse left click is released, it destroys cloned object called Magic
wand.Deactivated:Connect(function()
    if not charging then
        local magic = wand:FindFirstChild('Magic')
        magic:Destroy()
    end
end)

-- Part I keep changing and main part
function addPosition(magic, direction, amount)
    magic.End.CFrame = magic.End.CFrame + direction * amount
end
-- Localscript
local player = game:GetService('Players').LocalPlayer
local replicatedStorage = game:GetService('ReplicatedStorage')
local remoteEvent = replicatedStorage.Wand.RemoteEvent
local num = 0

while true do
    local mouse = player:GetMouse()

    remoteEvent:FireServer(mouse)
    num += 1

    --print('Loop ' .. num)
    task.wait(1)
end
1 Like