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
Also a LocalScript.
Try this: https://developer.roblox.com/en-us/articles/Fighting-Against-Lag