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

Why is my gun fire slower than what I put at the wait value?

Asked by 6 years ago

I am trying to make an automatic ak-47, but I am having trouble with the automatic part of the code. I want to know how to make a smooth fire rate and not one that is unpredictable like the one I have. I want to say the culprit is the repeat until loop. I think maybe the function call FireServer() is way to slow and is causing problems with my gun.

Local script

local player = game:GetService("Players").LocalPlayer
local tool = script.Parent
local event = tool:WaitForChild("RemoteEvent")
local stopped = true

tool.Equipped:Connect(function(mouse)
    mouse.Button1Down:Connect(function()
        stopped = false
        repeat
            event:FireServer(mouse)
            wait(1)
        until stopped
    end)

    mouse.Button1Up:Connect(function()
        stopped = true
    end)
end)

Server Script

local tool = script.Parent
local event = tool:WaitForChild("RemoteEvent")
local main = tool:WaitForChild("Main")
local fireSound = script.Parent.Handle.FireSound

local function CreateRay(player, mouse)
    local ray = Ray.new(main.Position, (mouse.Hit.p - main.Position).unit * 300)
    local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)
    return part, position
end

local function PlayEffects()
    fireSound:Play()
    main["1FlashFX[Smoke]"].Enabled = true
    main["FlashFX[Flash]"].Enabled = true
    main["FlashFX[Light]"].Enabled = true
    wait(0.1)
    main["1FlashFX[Smoke]"].Enabled = false
    main["FlashFX[Flash]"].Enabled = false
    main["FlashFX[Light]"].Enabled = false
end

local function CreateBeam(position)
    local beam = Instance.new("Part", workspace)
    beam.FormFactor = Enum.FormFactor.Custom
    beam.Material = Enum.Material.Neon
    beam.BrickColor = BrickColor.new("New Yeller")
    beam.Transparency = 0.5
    beam.Anchored = true
    beam.Locked = true
    beam.CanCollide = false

    local direction = (main.Position - position).magnitude
    beam.Size = Vector3.new(0.3, 0.3, direction)
    beam.CFrame = CFrame.new(main.Position, position) * CFrame.new(0, 0, -direction / 2)

    return beam
end

local function CalculateDamage(part)
    if part and part.Parent then
        local humanoid = part.Parent:FindFirstChild("Humanoid")

        if not humanoid then
            humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
        end

        if humanoid then
            humanoid:TakeDamage(10)
        end
    end
end

event.OnServerEvent:Connect(function(player, mouse)
    local part, position = CreateRay(player, mouse)
    local beam = CreateBeam(position)
    game:GetService("Debris"):AddItem(beam, 0.1)
    CalculateDamage(part)
end)
0
It might be roblox studio not able to keep up with the demand of making bullets and shooting it. iRexBot 147 — 6y
0
it is shooting one bullet per second though abnotaddable 920 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago

Waits can yield longer than specificed if roBlox is very busy and cannot process it. This happened to my gun that bounces off objects.It still happened when I shot only one bullet.

Ad

Answer this question