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

How would I make an automatic weapon?

I have a simple laser-pistol creating by ROBLOX (the one from the wiki) and I was wondering on how I would go about making it automatic without having to fire the RemoteEvent multiple time as that causes lag. I forgot to mention that I've already converted it to function with FE.

I've tried while loops and repeat loops but I can't for some reason make it work. Any suggestion?

  • Note : I'm not asking for a script, rather what method I would use to do this?

3 comments

0

Just saying, I didn't think a user would join roblox and make a game 3 days after they joined.

0

I joined ROBLOX in 2011, I registered for this site a while back but couldn't get the password and switch my main account to this one, it's hard to explain.

0

Firing a RemoteEvent multiple times does not cause lag. Maybe something else that you're doing is causing lag.

1 answer

A simple one would look like this:

(A local script inside a tool):

local FIRE_DELAY = 0.1

local Tool = script.Parent

local active = false
Tool.Activated:Connect(function()
	active = true
end

Tool.Deactivated:Connect(function()
	active = false
end

local function CanShoot()
	return active
end

local function Shoot()
	-- Shooting code goes here.
end

local lastFiredTime = tick()
game:GetService("RunService").Stepped:Connect(function()
	local timeNow = tick()
	if CanShoot() then
		if timeNow - lastFiredTime > FIRE_DELAY then
			Shoot()
			lastFiredTime = timeNow
		end
	end
end)

8 comments

0

Activated fires when the player clicks when the tool is equipped. You should use Equipped to get when the tool is equipped.

0

Also, why do you have an unnecessary function to check a variable value and not affecting the variable?

0

This wouldn't work server-sided, would it?

0

@hiimgoodpack: 1. Activated can only ever fire when the tool is equipped anyway. I don't need to check whether or not the tool is equipped.

0
  1. It is not necessary, but abstraction is a very good practice. I can check, for example, whether or not the player has enough ammo or if the player is dead inside that function.
0

@gmatchOnRoblox The reason why your game is lagging is not that you're firing RemoteEvents too fast. Look for optimizations somewhere else.

0

Firing RemoteEvents constantly can lag a game. Roblox talked about this several times. Sending that event takes up space other data could be taking advantage of(Physics are separate I think)

0

Unless you have some compelling proof, I disagree. I have empirical proof that it doesn't lag, just look at PF and, well, pretty much any other shooty game on Roblox that's FE (for PF, data sent between the client and server is even end-to-end encrypted, and it still doesn't lag.)

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