Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How would I make an automatic weapon?

Asked by 6 years ago
Edited 6 years ago

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?
0
Just saying, I didn't think a user would join roblox and make a game 3 days after they joined. hiimgoodpack 2009 — 6y
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. gmatchOnRoblox 103 — 6y
0
Firing a RemoteEvent multiple times does not cause lag. Maybe something else that you're doing is causing lag. XAXA 1569 — 6y

1 answer

Log in to vote
0
Answered by
XAXA 1569 Moderation Voter
6 years ago

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)
0
Activated fires when the player clicks when the tool is equipped. You should use Equipped to get when the tool is equipped. hiimgoodpack 2009 — 6y
0
Also, why do you have an unnecessary function to check a variable value and not affecting the variable? hiimgoodpack 2009 — 6y
0
This wouldn't work server-sided, would it? gmatchOnRoblox 103 — 6y
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. XAXA 1569 — 6y
View all comments (4 more)
0
2. 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. XAXA 1569 — 6y
0
@gmatchOnRoblox The reason why your game is lagging is not that you're firing RemoteEvents too fast. Look for optimizations somewhere else. XAXA 1569 — 6y
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) lukeb50 631 — 6y
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.) XAXA 1569 — 6y
Ad

Answer this question