Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How can I make this raycasting gun less laggy?

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

0

What's happening is that you're calculating a lot of Rays faster than the server can handle. Try increasing StepDistance and/or StepWait

0

Thanks, but there is still some lag.

0

Also, on the guy's video it shows NO lag.

1 answer

I'm assuming you have Filtering Enabled, disabled so...

Update the laser's position client-side (if there were 10 players on the server and the server had to update lasers for all of them, that would cause lag).

If not...

You're gonna have to add a system where the client asks the server to create a part (making it global so everyone can see it) and then update its position by setting its current CFrame to whatever the laser's CFrame is.

You'll need to use RemoteEvents.

Example:

--Script called "RemoteServer" (or whatever) located in workspace
--Assuming the global ray has already been created
script.UpdateRay.OnServerEvent:connect(function(Player,Ray,NewCFrame)
	Ray.CFrame = NewCFrame
end)
--LocalScript
--Call this when you're updating the laser

--Assuming that the global part has already been created
game.workspace.RemoteServer.UpdateRay:InvokeServer(GlobalRay,laser.CFrame)

1 comment

0

Why do you assume it was not FE? It clearly has remote events like n FE, but you guessed right, the thing I was making it for is not FE. You know what, this is confusing. Just ignore this if this is 2 confusing

This discussion is preserved as it was. New answers, comments, and votes are closed.