I am trying to create a beam towards your mouse when you click with raycasting, but i don't get expected results.

Script:

script.Parent.Wanded.OnServerEvent:Connect(function(player, hit)a
    local pos = hit.p
    
    local ray = Ray.new(script.Parent.Handle.Position, pos)
    
    local length = 20
    
    local hitted = workspace:FindPartOnRayWithIgnoreList(ray, {script.Parent.Handle, player.Character})
    
    local canDamage = false
    
    if hitted ~= nil then
        local size = (script.Parent.Handle.Position - hitted.Position).Magnitude
        
        if size > 20 then
            length = 20
            
            canDamage = false
        else
            length = size
            
            canDamage = true
        end
    else
        length = 20
        
        canDamage = false
    end
    
    
    local beam = Instance.new("Part", workspace.Beams)
    
    beam.Anchored = true
    
    beam.CanCollide = false
    
    beam.Material = Enum.Material.Neon
    
    local unit = (script.Parent.Handle.Position - pos).Unit
    
    beam.CFrame = CFrame.new(script.Parent.Handle.Position - Vector3.new(0, 0, length), unit)
    
    beam.Size = Vector3.new(1, 1, length)
end)

LocalScript:

local player = game.Players.LocalPlayer

local mouse = player:GetMouse()

local can = false

local debounce = true

script.Parent.Equipped:Connect(function(plays)
	can = true
end)

script.Parent.Equipped:Connect(function(plays)
	can = false
end)

mouse.Button1Down:Connect(function(plays)
	if can and debounce then
		script.Parent.Wanded:FireServer(mouse.Hit)
	end
end)

Gyazo:

https://gyazo.com/80d844f80f9dbddef5702ed8bb095a3f

Help pls

1 comment

0

Use UserInputService instead, getmouse is deprecated.