Ad
Log in to vote
0

What are some tips on optimizing a gun system?

Asked by 13 days ago
Edited 13 days ago

To not bore you people with the details, I'm working on a gun system with a server sided script in server-script-service. The issue is that traces occasionally just refuse to appear. The way the gun system works is that there is a single remote-event in replicated-storage, that event is fired by all the guns when being shot. Also, bare with me because I'm somewhat new.

Also, keep in mind that the server-sided script in server-script-service handles the shooting, damage handling and playing audio when reloading, which is why you see the "If not IsReloading then" line is there.

So, basically, my question is what are some ways of optimizing the gun system.

Example of the remote:

ShootRemote.OnServerEvent:Connect(function(player, tool, traceColor, mousepos, position)
    if not IsReloading then
        local ShootSound = tool.Handle:WaitForChild("Shoot")
        local MuzzleFlash = tool.Hole:WaitForChild("MuzzleEffect")
        local Hole = tool:WaitForChild("Hole")
        ShootSound:Play()

        MuzzleFlash.Enabled = true  

        local trace = Instance.new("Part")
        trace.Anchored = true
        trace.CanCollide = false
        trace.Transparency = 0.8
        trace.BrickColor = traceColor
        trace.Material = Enum.Material.Neon

        local distance = (Hole.CFrame.p - position).magnitude

        trace.Size = Vector3.new(0, 0, distance - 2)
        trace.CFrame = CFrame.new(Hole.CFrame.p, mousepos) * CFrame.new(0, 0, -         distance/2)
        trace.Parent = workspace

        wait(0.05)
        MuzzleFlash.Enabled = false

    end
end)

Answer this question