My raycasting gun bounces off walls, but I don't think that is causing lag, because when I shoot it into the sky, it still lags. If you want the script that explains how you can make bullets bounce of walls that I used, go to this website. I edited the code to this for a gun. Local Script:
local mouse = game:GetService('Players').LocalPlayer:GetMouse()
local held = false
local equipped = false
local handle = script.Parent.Parent.Handle
local content = game:GetService('ContentProvider')
content:Preload('rbxassetid://171140306') --Equipping sound
content:Preload('rbxassetid://143286342') --Firing sound
content:Preload('rbxassetid://169310310') --Uneqipping sound
script.Parent.Parent.Equipped:Connect(function()
equipped = true
handle.EquipSound:Play()
end)
script.Parent.Parent.Unequipped:Connect(function()
equipped = false
handle.UnequipSound:Play()
end)
mouse.Button1Down:Connect(function()
if equipped then
repeat
wait(.18)
print('Fire!')
script.Parent.RemoteEvent:FireServer()
handle.FireSound:Play()
until not held or not equipped
end
end)
mouse.Button1Up:Connect(function()
held = false
end)
I used a RemoteEvent to fire the gun quicker.
ServerScript:
script.Parent:WaitForChild('RemoteEvent').OnServerEvent:connect(function()
local laser = Instance.new("Part")
laser.Name = "Laser"
laser.FormFactor = Enum.FormFactor.Custom
laser.TopSurface, laser.BottomSurface = 0, 0
laser.Size = Vector3.new(0.2, 0.2, 0.2)
laser.BrickColor = BrickColor.Random()
laser.Anchored = true
laser.CanCollide = false
laser.Locked = true
laser.CFrame = script.Parent.CFrame
laser.Parent = game.Workspace
local maxDistance = 400
local curDistance = 0
local stepDistance = 8
local stepWait = .2
local currentPos = script.Parent.Position
local currentNormal = script.Parent.CFrame.lookVector
local function Step(overrideDistance)
-- Cast ray:
local ray = Ray.new(currentPos, currentNormal * (overrideDistance or stepDistance))
local hit, pos, norm = game.Workspace:FindPartOnRayWithIgnoreList(ray, {script.Parent})
-- Update laser position:
laser.Size = Vector3.new(0.4, 0.4, (pos - currentPos).magnitude)
laser.CFrame = CFrame.new(currentPos:lerp(pos, 0.5), pos)
local oldPos = currentPos
currentPos = pos
if (hit) then
-- r = d - 2(d DOT n)n
-- Reflect:
local reflect = (currentNormal - (2 * currentNormal:Dot(norm) * norm))
currentNormal = reflect
Step(stepDistance - (pos - oldPos).magnitude)
return
end
curDistance = (curDistance + (pos - oldPos).magnitude)
-- Apply fade effect to laser as it approaches max distance from < 75 studs:
if (curDistance > (maxDistance - 75)) then
local d = (curDistance - (maxDistance - 75)) / 75
laser.Transparency = d
end
-- Recurse if max distance not reached:
if (curDistance < maxDistance) then
wait(stepWait)
Step()
end
end
Step()
-- Done! Destroy laser:
laser:Destroy()
end)
If you use this in a part, or ANYTHING, it will absolutely lag, even if you shoot it into the sky and only shoot one bullet.

3 comments
What's happening is that you're calculating a lot of Rays faster than the server can handle. Try increasing StepDistance and/or StepWait
Thanks, but there is still some lag.
Also, on the guy's video it shows NO lag.