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

I’m trying to create a wand tool that shoots beams which follow the direction of the user’s mouse. At the moment, the beam either goes in a specific direction or doesn’t follow the mouse and stays at one point.
Code:

-- 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
                while (pcall(addPosition, magic, amount)) do
                    addPosition(magic, 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, amount)
    --magic.End.Position += Vector3.new(0, 0, amount)
    
    magic.End.CFrame = magic.End.CFrame + mousePosition.CFrame.LookVector * amount
    --magic.End.CFrame += mousePosition.CFrame.LookVector * 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.Hit.Position)
	--remoteEvent:FireServer(mouse)
	num += 1
	
	--print('Loop ' .. num)
	task.wait(1)
end

Image 1:
image
Video 1: Beam going in a specific direction

Video 2: Beam staying at one point, not moving and not following the mouse

  -- Main part here
    -- Video 1
    magic.End.Position += Vector3.new(0, 0, amount)
    
    -- Video 2
    magic.End.CFrame = magic.End.CFrame + mousePosition.CFrame.LookVector * amount
1 Like

couldnt you just use the mouse cframe and times it by the amount instead of adding its self to the math. I worded this weird:

[-] magic.End.CFrame = magic.End.CFrame + mousePosition.CFrame.LookVector * amount
[+] magic.End.CFrame = mousePosition.CFrame.LookVector * amount
1 Like

I don’t know what I am doing wrong, the wand does the same thing
Video:

local amount = 1000
while (pcall(addPosition, magic, amount)) do
	addPosition(magic, amount)
	task.wait(1)
end
--
function addPosition(magic, amount)
	magic.End.CFrame = mousePosition.CFrame.LookVector * amount
	-- magic.End.CFrame += mousePosition.CFrame.LookVector * amount <- is it meant to be this? or the one above?
end)
1 Like

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

well what was the error that it told you?

1 Like

Changed mousePosition.position to mousePosition and it worked, thank you so much for ur help guys, appreciate it.

in which script? have you done it?

1 Like

Server Script, it shoots the beam which it has never done before but I think I have to fix the mousePosition part, because it gets the position when it charges the wand instead of during when it is released.

1 Like

alright well if you need help in anything else let’s me know

Ah yeah remember to use the CFrame of the mouse when using mouse positions. That works better than just the position lol also np

How to stop the beam from going through objects like walls, this is what I have so far. Also is there a way to make it so you can make it change the direction instead of it going in a fixed direction of the original mousePosition.
Video 1:

				local continueRunning = true
				local endPoint = magic.End
				local direction = (mousePosition - magic.Start.Position).Unit
				local amount = 1
				
				endPoint.Touched:Connect(function(hit)
					if not hit:IsDescendantOf(wand) then
						continueRunning = false
					end
				end)
				
				while continueRunning do
					addPosition(endPoint, direction, amount)
					
					if not pcall(addPosition, endPoint, direction, amount) then
						continueRunning = false
					end
					task.wait(0.1)
				end

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 addPosition(magic, direction, amount) do
                    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)
    local ray = Ray.new(magic.End.Position, direction * amount)
    local part, position, normal = workspace:FindPartOnRay(ray, magic)
    
    if part and part:IsDescendantOf(workspace) then
        magic.End.Position = position
        return true
    end
    
    return false
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()
    local hitPosition = mouse.Hit.Position
    
    remoteEvent:FireServer(mouse, hitPosition)
    num += 1

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

try using a raycast and drawing it from the beam to where the mouse pos is and if it hits anything dont sent the data to the server

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.