Remote event fires twice inside a while loop for some reason

I’ve got a remote event, and that remote event is meant to shoot gun (yay!)

but that remote event fires TWICE at the same time for some reason (pensive)

instead of bumping my previous post, i’m gonna ask why this remote event fires twice, or well, everything in the while loop runs twice for some reason

AUTOMATIC = function(weapon: Tool, mouse: Mouse, shootAnim: Animation)
		local FIRERATE = gunStats[weapon.Name]["FIRERATE"]
		shooting = true
		
		local buttonUpConnection
		buttonUpConnection = mouse.Button1Up:Connect(function()
			disableIsHoldingEvent:FireServer()
			shooting = false
			buttonUpConnection:Disconnect()
		end)
		
		while shooting do
			if weapon.Bullets.Value < 1 then
				disableIsHoldingEvent:FireServer()
				shooting = false
				break
			end
			
			shootEvent:FireServer(mouse.Hit.Position)
			shootAnim:Play()
			print("i have fired remote event too many times because i have stupid lol!") -- this will print twice at once, which means everything in here runs twice for some reason
			ApplyRecoil(weapon)
			task.wait(FIRERATE)
		end
	end

Where do you call the Automatic function?

uh i call it slightly below the table of functions

table of functions in a table of functions, ironic i know

function shootingModule.Shoot(weapon: Tool, player: Player)
	local char = player.Character
	local humanoid = char:WaitForChild("Humanoid")
	local mouse = player:GetMouse()
	
	local animator = humanoid:FindFirstChildOfClass("Animator")	
	local shoot = weapon:WaitForChild("Shoot")
	
	local shootAnim = animator:LoadAnimation(shoot)
	shootFunctions[weapon.GunType.Value](weapon, mouse, shootAnim) -- Indexing right here!!!
end

everything is in a module script, and that module script gets called from a local script:

char.ChildAdded:Connect(function(child)
	if not child:IsA("Tool") then return end
	weapon = child
	if weapon.WeaponType.Value == "GUN" then	
		local Shooting = weapon:WaitForChild("Shooting")
		local Reloading = weapon:WaitForChild("Reloading")
		local Bullets = weapon:WaitForChild("Bullets")
		local Equipping = weapon:WaitForChild("Equipping")
		
		local Shoot = weapon:WaitForChild("Shoot")
		local FIRERATE = gunStats[weapon.Name]["FIRERATE"]
		
		downConnection = mouse.Button1Down:Connect(function()
			if Shooting.Value then return end
			if Reloading.Value then return end
			if Bullets.Value < 1 then return end
			if Equipping.Value then return end
			
			shootingModule.Shoot(weapon, plr)
		end)
	end		 
end)
1 Like

Ok and where is that script located and do you ever disconnect this function

1 Like

script is inside StarterCharacterScripts

and yes i do, but only whenever the tool gets unequipped

char.ChildAdded:Connect(function(child)
	if not child:IsA("Tool") then return end
	weapon = child
	if weapon.WeaponType.Value == "GUN" then	
		local Shooting = weapon:WaitForChild("Shooting")
		local Reloading = weapon:WaitForChild("Reloading")
		local Bullets = weapon:WaitForChild("Bullets")
		local Equipping = weapon:WaitForChild("Equipping")
		
		local Shoot = weapon:WaitForChild("Shoot")
		local FIRERATE = gunStats[weapon.Name]["FIRERATE"]
		
		downConnection = mouse.Button1Down:Connect(function()
			if Shooting.Value then return end
			if Reloading.Value then return end
			if Bullets.Value < 1 then return end
			if Equipping.Value then return end
			
			shootingModule.Shoot(weapon, plr)
		end)
	end		 
end)

char.ChildRemoved:Connect(function(child)
	if not child:IsA("Tool") then return end
	
	if downConnection then
		downConnection:Disconnect()
	end

	weapon = nil
	shooting = false
end)

No idea why its firing twice but I made a debounce system to prevent it from doing that, try implementing something like this to the function above:

local Debounces = {}

function Shoot(player)
	if not Debounces[player.Name] then
		Debounces[player.Name] = true
	end
	if Debounces[player.Name] == true then
		Debounces[player.Name] = false
		print("EEE")
		task.wait(10)
		Debounces[player.Name] = true
	end
end

you mean like, put this function somewhere then call it inside the while loop?

and replace the task.wait(10) with task.wait(FIRERATE)?

Kinda more like

local Debounces = {}

function shootingModule.Shoot(weapon: Tool, player: Player)
	if not Debounces[player.Name] then
		Debounces[player.Name] = true
	end
	if Debounces[player.Name] == true then
		Debounces[player.Name] = false

		local char = player.Character
		local humanoid = char:WaitForChild("Humanoid")
		local mouse = player:GetMouse()
		
		local animator = humanoid:FindFirstChildOfClass("Animator")	
		local shoot = weapon:WaitForChild("Shoot")
		
		local shootAnim = animator:LoadAnimation(shoot)
		shootFunctions[weapon.GunType.Value](weapon, mouse, shootAnim)

		task.wait(gunStats[weapon.Name]["FIRERATE"])
		Debounces[player.Name] = true
	end
end

Small issue i can see and that’s that if they player swaps to a different gun while on cooldown (if they can) the gun will be on cooldown for the old guns fire rate

no that doesn’t fix the issue sadly :pensive:

Is it possible if you could make a new place dump all the relevant items into it and uncopylock it then send the link? as I have genuinely no idea how to help other wise (unless you also don’t like sharing your scripts lol)

sure thing, give me a minute charrrrrr

omg i am pasting the assets and the gun DOESN’T work at all :sob:

Untitled Game.rbxl (191.5 KB)

I GOT IT WORKING
@floppofuss

replicated storage
server script service
starter character scripts

and some gui as a bonus

that’s where everything is located

i just removed all the animations as i wasn’t the owner of them so they wouldn’t play and end up breaking also turned up the fire rate to make it move obvious if there was an issue and I’m rarely getting the message printing 2 times

if you turned up the fire rate of the from the microscopic 0.075 does it still print 2 times at once?

huh, no

but the anti-cheat still triggers regardless, perhaps it’s something to do with the server script?

oh I see I increased the max bullets so I could test for longer and it triggered the anitcheat, I didn’t notice you have something setup to detect that

yeah i feel like some of those anti-cheats are unnecessary, but better safe than sorry

ill try re add the animations and see what that does

I’m not seeing any firing at the exact same time still?

the hell?

is your firerate still 0.075?

is this just an acoustic moment?