Making a gun system - would this kill the server/client?

-- Variables
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local mouseEvent = script.Parent:WaitForChild("MouseEvent")
local automaticMode = false -- Toggle for automatic mode
local fireRate = 0.1

-- Function to fire bullets
local function fireBullet()
	mouseEvent:FireServer(mouse.Hit.Position)
end

-- Function to handle mouse button input
local function onMouseButtonDown()
	if automaticMode then
		while mouseButton1Down do
			fireBullet()
			wait(fireRate) -- Adjust the rate of automatic fire as needed
		end
	else
		fireBullet()
	end
end

-- Detecting mouse button down event
mouse.Button1Down:Connect(function()
	mouseButton1Down = true
	onMouseButtonDown()
end)

-- Detecting mouse button up event (to stop automatic fire)
mouse.Button1Up:Connect(function()
	mouseButton1Down = false
end)

I’m just concerned about the event, if it fires whenever it’s held down, would it just lag/destroy the server basically?

How could I mitigate that?

Created a new iteration of the script with ticks and stuff:

-- Variables
local tool = script.Parent
local player = game:GetService("Players").LocalPlayer or game:GetService("Players").PlayerAdded:Wait()
local char = player.Character or player.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
local root = char:WaitForChild("HumanoidRootPart")
local mouse = player:GetMouse()
local mouseEvent = script.Parent:WaitForChild("MouseEvent")
local currentCamera = workspace.CurrentCamera
local automaticMode = true -- Toggle for automatic firing mode
local firing = false -- Debounce to prevent spamming
local active = false


-- Fire rate control (adjust as necessary)
local fireRate = 0.1
local lastFireTime = 0



-- ShiftLock Toggle
function toggleShiftLock(active) 
	if active then 
		---------------------------------------------------------
		game:GetService("RunService"):BindToRenderStep("ShiftLock", Enum.RenderPriority.Character.Value, function()
			game:GetService("UserInputService").MouseBehavior = Enum.MouseBehavior.LockCenter 
			
			local _, y = workspace.CurrentCamera.CFrame.Rotation:ToEulerAnglesYXZ() --Get the angles of the camera
			root.CFrame = CFrame.new(root.Position) * CFrame.Angles(0,y,0) 
			
			hum.AutoRotate = false
			hum.CameraOffset = Vector3.new(1.75,.5,0) 
			
			mouse.Icon = "rbxassetid://12829852445"
		end) 
		---------------------------------------------------------
	elseif not active then
		---------------------------------------------------------
		game:GetService("RunService"):UnbindFromRenderStep("ShiftLock")
		game:GetService("UserInputService").MouseBehavior = Enum.MouseBehavior.Default 
		
		hum.AutoRotate = false
		hum.CameraOffset = Vector3.new(0,0,0) 
		
		mouse.Icon = ""
	end
end


-- Function to fire bullets
local function fireBullet()
	if tick() - lastFireTime >= fireRate then
		mouseEvent:FireServer(mouse.Hit.Position)
		lastFireTime = tick()
	end
end

-- Function to handle mouse button input
local function onMouseButtonDown()
	if automaticMode then
		while mouseButton1Down do
			fireBullet()
			wait(fireRate) -- Ensuring bullets are fired at a controlled rate
		end
	else
		fireBullet()
	end
end

-- Detecting mouse button down event
mouse.Button1Down:Connect(function()
	mouseButton1Down = true
	onMouseButtonDown()
end)

-- Detecting mouse button up event (to stop automatic fire)
mouse.Button1Up:Connect(function()
	mouseButton1Down = false
end)

-- Detecting if tool equipped to change mouse/cam
tool.Equipped:Connect(function()
	active = true
	toggleShiftLock(active)
end)

-- Detecting if tool unequipped to change mouse/cam
tool.Unequipped:Connect(function()
	active = false
	toggleShiftLock(active)
end)

Yes, parts of code were done by the aid of ChatGPT, however, it has proven to be an incredibly useful tool for avoiding cumbersome situations or when no one else helps.

This should be fine. A simple loop with an interval of 0.1s likely isn’t enough to create lag on its own, even with firing a remote.

I don’t think they suit this case, but if you ever need to fire remotes really fast, then try use UnreliableRemoteEvents. They’re just like remote events, except they’re more performant because they don’t incorporate ordering or event re-sending. See more:

1 Like

Don’t they just have a chance to not fire, or something along the lines?

Practically yes. Unlike normal RemoteEvents, they don’t resend lost packets, so your requests have a chance to not go through. They should only be used when fired often and ordering doesn’t matter, so that if a packet is lost then the next request should approximately fill its place. This same concept is used in almost all videogames.

Ah, I see, thank you! (the key limit)

1 Like

All good :+1:. Also, unless your gun is shooting exceptionally fast, don’t use unreliable remotes for firing. Nothing is more frustrating than hits not registering.

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