How can I make my automatic weapon better?

Hello I tried to make an automatic riffle in my game with coroutine
I’ve done that

local function autoFire()
    local replicateRate = 6
    local counter = 0

    autoFireCoroutine = coroutine.create(function()
        while equipped and weaponData.ammo.Value > 0 and weaponData.Configs.Info:GetAttribute("currentShootingMode") == "automatic" do
            counter += 1
            local shouldReplicate = counter % replicateRate == 0
            fire(shouldReplicate)
            task.wait(defaultStats:GetAttribute("baseAutomaticFireDelay"))
        end
    end)

    coroutine.resume(autoFireCoroutine)
end

but i’m not sure if this is the best, and most optimized way

Thanks!

Avoid creating closures and spamming tasks “just because you can.”
Threads are pretty expensive, and I assume that you connected this function to the event there, so it’s already in a separate thread; no need of creating another one.
If possible, I suggest using parallel luau carefully.
Raycasting on both the client and server can help to determine bullet direction on the server side more accurately.
Etc., you send Vector3 to the server (where you hit with a bullet), and then on the server you calculate (PlayerPosition - This vector3 you received from remote).Unit to get proper direction.

If you are raycasting on server then theres absolutely no reason to do so on client, it is already expensive to raycast on server, talk about automatic weapon.

Nope, you have fully dismissed the entire point.
You can’t really calculate direction otherwise safely unless your bullet system tracks movement velocity.
By giving position to the server instead of just direction, you are able to dynamically adjust direction as to where the player was supposed to be shooting, and this accounts for movement, etc.
Also, since when has raycasting become expensive?
Even Doom uses raycasting. What are you even talking about?

I can clearly see that you are neither a gamer nor a game developer.
That is not how games implement shooting, especially for people with high ping or really any game that doesn’t require you to stand idly by without any movement.
Also please avoid sending multiple messages and instead use quotes if you want to answer a specific question.

I couldn’t care less about high ping, theres a choice of better servers for this exact reason

Hello, could you please process my information instead of deflecting and sending a repulsive response?
Thank you.

sorry for the delay
To be honest i did not understood anything in your message :smiling_face_with_tear:
But this code run on the client (dont worry i made checks on the server to prevent cheaters but this is the best way i found to reduce delay and server load)

Oh also, yeah this function is connected to my tool activated function

connect the user’s input to a boolean. when mouse down, boolean true. when mouse up, boolean false. while boolean is true, fire rounds at a set cooldown

Oh,

That’s simple, why didn’t I think of that?

Thanks!