Help needed on Automatic FastCast Gun

Hello Developers!

I was recently working on a game which is focused on fighting and PvP using guns. I have created all the guns, except for the fact that all of them all are Semi-Automatic (Click to shoot), and I just created an automatic script for them. The problem with the script is that it doesn’t play the fire sound every time it is fired.
Forgot to mention: The gun does work fully automatically, the only problem is the firing sound, it only plays in the beginning of the mousebuttondown and end of mousebuttonup

Here’s the full script:


local tool = script.Parent
local WeaponTool = tool:WaitForChild("Gun")
local UIS = game:GetService("UserInputService")
local RS = game:GetService("RunService")
local mouse = game.Players.LocalPlayer:GetMouse()

local idle = "rbxassetid://9212173767"
local reload = "rbxassetid://9212239484"
local fire = "rbxassetid://9212167959"
local out = "rbxassetid://9212173767"
local inspect = "rbxassetid://9212173767"

local plr = game.Players.LocalPlayer

local data = game:GetService("ReplicatedStorage").Remote.Data
local ammo = script.Parent.Ammo
local leftammo = script.Parent.Left

local Sfire = script.Parent:WaitForChild("Sounds / Config").Fire
local SReload = script.Parent:WaitForChild("Sounds / Config").reload
SReload.PlaybackSpeed = 1

local lolboom = game:GetService("StarterGui")

local reloading = false
local inspecting = false
local down = false -- used for automatic

local firerate = 1 -- delay



local function reloadgun()
	if not reloading and leftammo.Value >= 1 then
		reloading = true
		local char = script.Parent.Parent

		local anim = Instance.new("Animation", char)
		anim.AnimationId = reload
		local human = char:FindFirstChild("Humanoid")
		local valid = human:LoadAnimation(anim)
		valid.Priority = Enum.AnimationPriority.Action4
		valid:Play()
		SReload:Play()

		ammo.Value = script.Parent.Maxammo.Value
		leftammo.Value -= script.Parent.Maxammo.Value

		valid.Stopped:Connect(function()
			data:FireServer(ammo.Value, leftammo.Value, tool.Name)
			reloading = false
		end)
		
	end

end

local function inspectgun()
	
	if not reloading and inspecting then
		inspecting = true
		local char = script.Parent.Parent

		local anim = Instance.new("Animation", char)
		anim.AnimationId = inspect
		local human = char:FindFirstChild("Humanoid")
		local valid = human:LoadAnimation(anim)
		valid.Priority = Enum.AnimationPriority.Action4
		valid:Play()
		
		valid.Stopped:Connect(function()
			
			inspecting = false
			
		end)

	end
	
end

script.Parent.Equipped:Connect(function()
	local char = script.Parent.Parent
	game.ReplicatedStorage.Remote.ConnectM6D:FireServer(WeaponTool.BodyHand)
	char["Right Arm"].ToolGrip.Part0 = char["Right Arm"]
	char["Right Arm"].ToolGrip.Part1 = WeaponTool.BodyHand
	
	local anim = Instance.new("Animation", char)
	anim.AnimationId = idle
	local human = char:FindFirstChild("Humanoid")
	local valid = human:LoadAnimation(anim)
	valid.Priority = Enum.AnimationPriority.Action4
	valid:Play()
	
	tool.Unequipped:Connect(function()
		game.ReplicatedStorage.Remote.DisconnectM6D:FireServer()
		valid:Stop()
	end)
	
	data:FireServer(ammo.Value, leftammo.Value, script.Parent.Maxammo.Value, tool.Name)

	
end)

tool.Unequipped:Connect(function()
	game.ReplicatedStorage.Remote.DisconnectM6D:FireServer()
	
end)

RS.RenderStepped:Connect(function()
	
	while not reloading and not inspecting and ammo.Value >= 1 and down do
		local char = script.Parent.Parent

		local anim = Instance.new("Animation", char)
		anim.AnimationId = fire
		local human = char:FindFirstChild("Humanoid")
		local valid = human:LoadAnimation(anim)
		valid.Priority = Enum.AnimationPriority.Action4
		valid:Play()

		local pos = mouse.Hit.Position
		
		Sfire:Play()
		ammo.Value -= 1
		tool.Funni:FireServer(pos)
		data:FireServer(ammo.Value, leftammo.Value, script.Parent.Maxammo.Value, tool.Name)
		
		task.wait(firerate)	
		
		valid.Stopped:Wait()

		
	end
	
end)

UIS.InputBegan:Connect(function(input, GPE)
	
		
	if input.UserInputType == Enum.UserInputType.Keyboard then
			
		if input.KeyCode == Enum.KeyCode.R and not plr.Backpack:FindFirstChild("Blocky Carbine") then
				
			reloadgun()
			
		elseif input.KeyCode == Enum.KeyCode.F and not plr.Backpack:FindFirstChild("Blocky Carbine") then
			
			print("INSPECTING")
			
			if not reloading and inspecting then
				inspecting = true
				local char = script.Parent.Parent

				local anim = Instance.new("Animation", char)
				anim.AnimationId = inspect
				local human = char:FindFirstChild("Humanoid")
				local valid = human:LoadAnimation(anim)
				valid.Priority = Enum.AnimationPriority.Action
				valid:Play()

				valid.Stopped:Connect(function()

					inspecting = false

				end)

			end
			
		end
		
	elseif input.UserInputType == Enum.UserInputType.MouseButton1 and not plr.Backpack:FindFirstChild("Blocky Carbine") then
		
		down = true
		
			
	end
		
	
end)

UIS.InputEnded:Connect(function(input, gpe)
	
	if input.UserInputType == Enum.UserInputType.MouseButton1 and not plr.Backpack:FindFirstChild("Blocky Carbine") then
		
		down = false
				
	end
	
	
end)

Any Help is Appreciated,
OceanTubez

2 Likes

You could try cloning and destroying sounds right after assigning .PlayOnRemove property to them. For sure that wouldn’t be quite good for the optimization, but the solution concept could work.

I did want to try that, but since this is a PvP game, a lot of players are gonna be shooting at the same time, I could try handling cloning and destroying on the Client perhaps? The script I listed is the Client script.

Client sounds wouldn’t bring the effect to the server meaning only the local player would be able to hear multiple shots at once.

The problem, as I understand, consists of the audio being longer than the delay between the shots. You could try reducing audio length and potentially adding a bigger delay between the shots.

2 Likes

my audio’s length is 0.3 seconds and my delay is 1 second.

Don’t know how it doesn’t work.

My automatic weapon scripts do not use RenderStepped but a repeat loop inside of which I have all the functionality. Perhaps try to re-program your specific part of the code responsible for the index and response to events fired upon the mousebutton being pressed down?

1 Like