Debounce Not Working COrrectly

My stamina value for some reason drops by a large amount , and is able to go to negative numbers despite my checks. I’m not sure why but heres the code :
This is my local scripting that handles the firing.

--local script
local player = game.Players.LocalPlayer
local Tool = script.Parent
local RS = game:GetService("ReplicatedStorage")
local Functions = RS:WaitForChild("Functions")
local char = player.Character
local UIS = game:GetService("UserInputService")


local BeamStaminaCost = 25
local BeamDmg = 25
local beamCD = 1
local beamShoot = true
local BeamEvent = Functions:WaitForChild("DarkMatter"):WaitForChild("DarkMatterBeam")

local BulletBlastEvent = Functions:WaitForChild("DarkMatter").BulletBlast
local BulletBlastStaminaCost = 5
local BulletBlastShoot = true
local BulletBlastCD = 3
local BulletBlastDamage = 10

local PlayerStamina = player.Stamina.Value




Tool.Equipped:Connect(function()

	UIS.InputBegan:Connect(function(input,IsTyping)
	
		
		PlayerStamina = player.Stamina.Value

	if IsTyping == false then
			PlayerStamina = player.Stamina.Value

	
		
			if input.KeyCode == Enum.KeyCode.F then
				PlayerStamina = player.Stamina.Value

				if BulletBlastShoot == true  and (PlayerStamina - BulletBlastStaminaCost) > 0  then

				BulletBlastShoot = false
				local mouse = player:GetMouse()
				local Target = mouse.Hit.p
				
				BulletBlastEvent:FireServer(Target,BulletBlastDamage,BulletBlastStaminaCost)
				
				
			end
		end
			if input.KeyCode == Enum.KeyCode.T then 
				 PlayerStamina = player.Stamina.Value

				if beamShoot == true   and (PlayerStamina - BeamStaminaCost)  >  0 then

				beamShoot = false
				local mouse = player:GetMouse()
				local target = mouse.Hit.p


				BeamEvent:FireServer(target,BeamDmg, BeamStaminaCost)


			end



		end
		
		
		

	end
	
	
	
	
end)
end)

BeamEvent.OnClientEvent:Connect(function()

	beamShoot = false


	delay(beamCD,function()
		beamShoot = true
	end)

end)

BulletBlastEvent.OnClientEvent:Connect(function(bool)
	
	BulletBlastShoot = false
	if bool  then 
		BulletBlastShoot = false
		delay(beamCD,function()
			BulletBlastShoot = true
		end)
	elseif bool == nil then 
		
		BulletBlastShoot = true
		
	end
	

end)






Serverscript for one of the codes:

local RS = game:GetService("ReplicatedStorage")
local Functions = RS:WaitForChild("Functions")
local TS = game:GetService("TweenService")
local Clones = RS:WaitForChild("Clones")
local BulletFolder = game.Workspace.BulletFolder
local DebrisFolder = game.Workspace.DebrisFolder
local EffectCD = 4

local BulletBlastRemote = Functions:WaitForChild("DarkMatter"):WaitForChild("BulletBlast")
local FastCast = require(RS.Modules.FastCastRedux)
local Manager = require(RS:WaitForChild("Modules"):WaitForChild('Manager'))
local function Bullet(player:Player, mousePos:CFrame,dmg:IntValue)
	
	
	local Range = 100
	
	local humrp= player.Character.HumanoidRootPart
	
	local NewBullet = Clones:WaitForChild("DarkMatterBullet"):Clone()
	NewBullet.Parent = BulletFolder
	NewBullet.CFrame = humrp.CFrame + humrp.CFrame.LookVector 
	NewBullet.CFrame = CFrame.new(NewBullet.CFrame.p,mousePos)
	
	local BV = Instance.new("BodyVelocity",NewBullet)
	BV.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
	BV.Velocity = NewBullet.CFrame.LookVector * 400
	BV.P = math.huge
	
	delay(0.25,function()	
		NewBullet:Destroy()
	end)
	
	local Params = RaycastParams.new()
	Params.FilterDescendantsInstances = {humrp.Parent, BulletFolder}
	Params.FilterType = Enum.RaycastFilterType.Blacklist
	
	local result = workspace:Raycast(NewBullet.Position,NewBullet.CFrame.LookVector * 100,Params)
	
	if result  then
		
		local part = result.Instance
		print(part)
		local humanoid = part.Parent:FindFirstChild("Humanoid") or part.Parent.Parent:FindFirstChild("Humanoid")

		if humanoid then
			humanoid:TakeDamage(10)
			
			
			
			local char:Model = part.Parent
			if char.UpperTorso:FindFirstChild("BulletBlast") then
			else
				local VFX = Clones.Particles.AttatchmentContainer.BulletBlast:Clone()
				VFX.Parent = char.UpperTorso
				VFX.Name = "BulletBlast"
				VFX.ParticleEmitter:Emit(1)
				game.Debris:AddItem(VFX,EffectCD)
			end
			
			local player:Player = game.Players:GetPlayerFromCharacter(char) 
			if player then 
				local Event = RS.Functions.ScreenChange
				Event:FireClient(player,24, Color3.fromRGB(72, 36, 108), EffectCD)


			end

		
			
			
		

		end
		

		if not result then
			
			
		end
		
	
	end
	
	
	
	
end

local count = 0

local OriginalFire = os.time()



BulletBlastRemote.OnServerEvent:Connect(function(player,mousePos,dmg,staminaCost)
	local profile = Manager.Profiles[player]
	if not profile then return end
	
	
	if count == 0 then
		player.Stamina.Value -= staminaCost

		OriginalFire = os.time()

	end
	
	if count < 3 then 
	

		
		local CurrentFire = os.time()
		if (CurrentFire - OriginalFire) < 3 then 
		Bullet(player,mousePos,dmg)
		count += 1
			BulletBlastRemote:FireClient(player)
		else 
				count =  0
		end
				
		
			
			
	
	
		
	--print("sup")
	
	
	count = 0
		BulletBlastRemote:FireClient(player,true)
	end
	
end)
1 Like

Sorry I am not able to understand enough to fix the debounce, however in your local script I believe that you should change the “local char = player.Character” to a word other than “char” as you can see in the code it is highlighted as blue which means it is a roblox keyword and should not be used for variable names.

“char” is not a keyword in Roblox or Lua (Luau specifically as well, which is what roblox uses).

It is just highlighted blue on the dev forum for some strange reason.
Same thing with the word “status”

2 Likes

Here, cleaned up your client code.

--- Services ---
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")

--- Private Variables ---
local LocalPlayer = Players.LocalPlayer
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local Mouse = LocalPlayer.Mouse
local FunctionsFolder = ReplicatedStorage:WaitForChild("Functions")
local Tool = script.Parent

local Beam = {
	["Damage"] = 25,
	["StaminaCost"] = 25,
	["Cooldown"] = 1,
	["Shoot"] = true
}

local BulletBlast = {
	["Damage"] = 10,
	["StaminaCost"] = 5,
	["Cooldown"] = 3,
	["Shoot"] = true
}

local PlayerStamina = LocalPlayer.Stamina.Value

--- Events ---
local DarkMatter = {
	["Beam"] = FunctionsFolder:WaitForChild("DarkMatter"):WaitForChild("DarkMatterBeam"),
	["BulletBlast"] = FunctionsFolder:WaitForChild("DarkMatter"):WaitForChild("BulletBlast")
}

--- Private Functions ---
local function onToolEquipped()
	local connection
	connection = UserInputService.InputBegan:Connect(function(inputObj, processed)
		if not processed and inputObj.KeyCode == Enum.KeyCode.F then
			PlayerStamina = LocalPlayer.Stamina.Value

			if BulletBlast.Shoot and (PlayerStamina - BulletBlast.StaminaCost) >= 0 then
				BulletBlast.Shoot = false
				local Target = Mouse.Hit.p
				DarkMatter.BulletBlast:FireServer(Target, BulletBlast.Damage, BulletBlast.StaminaCost)

				task.delay(BulletBlast.Cooldown, function()
					BulletBlast.Shoot = true
				end)
			end
		end
		if not processed and inputObj.KeyCode == Enum.KeyCode.T then
			PlayerStamina = LocalPlayer.Stamina.Value

			if Beam.Shoot and (PlayerStamina - Beam.StaminaCost) >= 0 then
				Beam.Shoot = false
				local Target = Mouse.Hit.p
				DarkMatter.Beam:FireServer(Target, Beam.Damage, Beam.StaminaCost)

				task.delay(Beam.Cooldown, function()
					Beam.Shoot = true
				end)
			end
		end
		connection:Disconnect()
	end)

	local unequipConnection
	unequipConnection = Tool.Unequipped:Connect(function()
		connection:Disconnect()
		unequipConnection:Disconnect()
	end)
end

--- Connections ---
Tool.Equipped:Connect(onToolEquipped)

Don’t know if this fixes your issue, however I did fix your memory leak.
Your server script also needs some fix as I can see however eh, that takes time.

2 Likes

whats a memory leak? also tbh i just redid it and it worked odly but yea .

what did you change in client that was a big deal??

Nothing much. I cleaned up the code and fixed various mistakes I otherwise wouldn’t be able to explain.
A memory leak happens when your code continues to take memory and is not being cleared up. (This is an incorrect explanation but to make it simple I said it like this)

Essentially, you did this:

This means, every time the tool is equipped, it opens a new connection for the InputBegan event. Connections take up memory, and here, every time you equip the tool, a new connection is created.

If you have functions that contains connections, and these functions are bond to repeat (like a game round), make sure to disconnect them after you’re done using them. To disconnect a connection, you can call the :Disconnect() method on a RBXScriptConnection type:

local UserInputService = game:GetService("UserInputService")
function Round()
    UserInputService.InputBegan:Connect(function(input)

    end)
end
-- This is a memory leak. Every time you call Round(), a new connection is made.
-- Simply put, if you call Round() 100 times, you will have 100 connections.
-- To fix that, we can use a local variable to store the connection,
-- then disconnect it when we don't need it anymore.
function Round()
    local connection = UserInputService.InputBegan:Connect(function(input)

    end)
    -- Disconnect the connection when we don't need it anymore.
    connection:Disconnect()
end
1 Like

How important are memory leaks , and what efffects do they have?

Well, a memory leak continues to use allocated memory without releasing the unneded memory, and it does have its consequences. Not to be confused with a space leak, which continues to take memory, but at one point it gets released.

Memory leaks, once spotted, must be fixed. Roblox provides tools to analyse scripts so you can find out where the source of the leak is. They’re as important as keeping a good script performance.

Most (especially beginner) scripters are simply happy that their script works, even if the code is horrible (looking at those who watch AlvinBlox). They apply the (unofficial) #1 rule of programming: If it works, don’t touch it. For large games, keeping a performant script is as important as keeping the game itself playable.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.