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

Gun fire sound is delayed?

I'm expanding off of Roblox's original laser gun tutorial. I've made it FE compatible, along with a bunch of other things. The problem is is that the fire sound of the gun is delayed for a bit, and then plays. It works perfectly in studio, but in game it's delayed. Here's the game link and the fire function.

[EDIT] ServerEvent caused lag for the sound to play, so I'm going to look up how to fix it. Thanks for the answers!

https://web.roblox.com/games/4744242219/

function fire()
	if reloading == false and currentAmmo > 0 then
	currentAmmo = currentAmmo - 1
	if player.PlayerGui:FindFirstChild("GunGUI") then
		player.PlayerGui.GunGUI.TextLabel.Text = "Ammo = ".. currentAmmo
	end
        game.ReplicatedStorage.PlaySound:FireServer(tool.Handle.FireSound)
  if FireAnimation then
	local fireAnim = Instance.new("Animation")
    fireAnim.AnimationId = FireAnimationID
    fireAnim.Name = "FireAnimation"
    fireAnim.Parent = tool
	game.ReplicatedStorage.PlayAnim:FireServer(tool.FireAnimation)
end
  local ray = Ray.new(tool.Handle.CFrame.p, (mouse.Hit.p - tool.Handle.CFrame.p).unit * 300)
  local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)

  local beam = Instance.new("Part", workspace)
  beam.BrickColor = BrickColor.new("White")
  beam.FormFactor = "Custom"
  beam.Material = "SmoothPlastic"
  beam.Transparency = 0.75
  beam.Anchored = true
  beam.Locked = true
  beam.CanCollide = false

  local distance = (tool.Handle.CFrame.p - position).magnitude
  beam.Size = Vector3.new(0.3, 0.3, distance)
  beam.CFrame = CFrame.new(tool.Handle.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)

  game:GetService("Debris"):AddItem(beam, 0.1)

  if part then
    print(part.Name)
    local humanoid = part.Parent:FindFirstChild("Humanoid")

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

    if humanoid then
	if part.Name == "Head" and headshotMultiplier == true then
		game.ReplicatedStorage.InflictTarget:FireServer(damage * headshotMultiplyBy, humanoid)
	else
      game.ReplicatedStorage.InflictTarget:FireServer(damage, humanoid)
end
    end
  end
end
end

2 comments

0

Also a LocalScript.

2 answers

✓ Accepted answer

Well it's because it checks for if the user doesn't have 0 ammo then it removes 1 ammo from the current ammo's the user has then it shows you the ammo's you have left, and then it fires the play sound remote event which makes the sound so, this should work!

function fire()
    if reloading == false and currentAmmo > 0 then
    currentAmmo = currentAmmo - 1
   game.ReplicatedStorage.PlaySound:FireServer(tool.Handle.FireSound)
    if player.PlayerGui:FindFirstChild("GunGUI") then
        player.PlayerGui.GunGUI.TextLabel.Text = "Ammo = ".. currentAmmo
    end
  if FireAnimation then
    local fireAnim = Instance.new("Animation")
    fireAnim.AnimationId = FireAnimationID
    fireAnim.Name = "FireAnimation"
    fireAnim.Parent = tool
    game.ReplicatedStorage.PlayAnim:FireServer(tool.FireAnimation)
end
  local ray = Ray.new(tool.Handle.CFrame.p, (mouse.Hit.p - tool.Handle.CFrame.p).unit * 300)
  local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)

  local beam = Instance.new("Part", workspace)
  beam.BrickColor = BrickColor.new("White")
  beam.FormFactor = "Custom"
  beam.Material = "SmoothPlastic"
  beam.Transparency = 0.75
  beam.Anchored = true
  beam.Locked = true
  beam.CanCollide = false

  local distance = (tool.Handle.CFrame.p - position).magnitude
  beam.Size = Vector3.new(0.3, 0.3, distance)
  beam.CFrame = CFrame.new(tool.Handle.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)

  game:GetService("Debris"):AddItem(beam, 0.1)

  if part then
    print(part.Name)
    local humanoid = part.Parent:FindFirstChild("Humanoid")

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

    if humanoid then
    if part.Name == "Head" and headshotMultiplier == true then
        game.ReplicatedStorage.InflictTarget:FireServer(damage * headshotMultiplyBy, humanoid)
    else
      game.ReplicatedStorage.InflictTarget:FireServer(damage, humanoid)
end
    end
  end
end
end

If it does help accept & like.

1 comment

0

The sound is still delayed in game, but the guy below says it fixed an issue I didn't know it had.

  1. Listen to the sound of the gun a couple of times. It could be the guns sound you used that actually has the delay because sometimes delay on sounds arn't good on custom sounds made by players

  2. Reply on this guys script above, He actually fixed an issue with ammo and not the sound...

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