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

How to prevent functions from being repeatedly called?

Ok, so I was making a Lightsaber and I came upon this problem. Heres the code..

local remoteStorage = game:GetService("ReplicatedStorage"):WaitForChild("RemoteHandlers",3)
 function getRemotes(remoteName, remoteType)
	for i, v in pairs(remoteStorage:GetChildren()) do
		if v and v.Name == remoteName and v.ClassName == remoteType then
			
			return v
		end
	end
	warn("Remotes not found, nil will be returned")
	return nil
end

--LocalScript
function OnEquipped(mouse)
	getRemotes("ColorSet","RemoteEvent"):FireServer(false,false,Tool.OuterBlade1,nil,nil,nil,Tool.Blade1,nil,nil,nil)
	--function setupColor(plr,isDouble,isDual,outer1,outer2,outer3,outer4,blade1,blade2,blade3,blade4)
	local idleAnim = Setting.Idle
	idleTrack = humanoid:LoadAnimation(idleAnim)
	idleTrack:Play()
	local BlockAnim = humanoid:LoadAnimation(Setting.Block)
	local function OnButton1Down()
		if get(player,boolCanAttack) == false then
			print("Cannot Attack")
		end
		if humanoid and boolCanAttack.Value == true and boolCanDamage.Value == false and Stunned.Value == false then
			if clicks == 1 then clicks = 2 else clicks = 1 end
			boolCanAttack.Value = false
			
			local attackAnimations = {Setting.Attack1,Setting.Attack2}
			local attackSound = {Handle.Slash1,Handle.Slash2}
			local attackAnim = humanoid:LoadAnimation(attackAnimations[clicks])
			local sound = attackSound[clicks]
			attackAnim:Play()
			sound:Play()
			boolCanDamage.Value = true
			local attackRemote = getRemotes("attackRemote","RemoteEvent")
			attackRemote:FireServer(boolCanDamage.Value,boolCanAttack.Value,Setting.Damage.Value,hitBox,nil,nil,nil,false,false) --getDamage(player,canDamage,canAttack,damageValue,damagePart,damagePart2,damagePart3,damagePart4,isDual,isDouble)
			wait(Wait)
			
			boolCanDamage.Value = false
			if Stunned.Value ~= true then
				boolCanAttack.Value = true
				else boolCanAttack.Value = false
			end
			if boolBlocking.Value == false then
				idleTrack:Play()
			end
		end
	end

-- Server Script
----------------Attacks
function getDamage(player,canDamage,canAttack,damageValue,damagePart,damagePart2,damagePart3,damagePart4,isDual,isDouble)
		 print(1000)
		local connection1
		connection1 = damagePart.Touched:connect(function(hit)
			local ff = hit.Parent:FindFirstChild("Blocker")
			local hum = hit.Parent:FindFirstChild("Humanoid")
			print("Activated")
			if hum  and canDamage == true and ff == nil then
				print("Attacked")
				canDamage = false
				hum:TakeDamage(damageValue)
				connection1:Disconnect()
			end
			
			
				if ff --[[and player.Block.Value > 0]] and player.CanAttack.Value == true and damagePart.Parent.Stunned.Value == false   then
					canAttack = false
					canDamage = false
					print("how!")
					getRemotes("Stunner","RemoteEvent"):FireClient(player)
					
					local clashLight = damagePart.Parent.Blade1.ClashLight
					local sparks = damagePart.Parent.Blade1.Sparks
					clashLight.Enabled = true
					sparks.Enabled = true
					wait(.1)
					clashLight.Enabled = false
					sparks.Enabled = false
					connection1:Disconnect()
		
				end
			
		end)
	
		if isDual == true then
				 local connection2
				connection2 = damagePart2.Touched:connect(function(hit)
				local hum = hit.Parent:FindFirstChild("Humanoid")
				if hum and canDamage == true then
					canDamage = false
					hum:TakeDamage(damageValue)
					connection2:Disconnect()
				end
			end)
		end
		
		if isDouble == true then
				 local connection3
				connection3 = damagePart3.Touched:connect(function(hit)
				local hum = hit.Parent:FindFirstChild("Humanoid")
				if hum and canDamage == true then
					canDamage = false
					hum:TakeDamage(damageValue)
					connection3:Disconnect()
				end
			end)
		end
		if isDouble == true then
				 local connection4
				connection4 = damagePart4.Touched:connect(function(hit)
				local hum = hit.Parent:FindFirstChild("Humanoid")
				if hum and canDamage == true then
					canDamage = false
					hum:TakeDamage(damageValue)
					connection4:Disconnect()
				end
			end)
		end
	end
getRemotes("attackRemote","RemoteEvent").OnServerEvent:Connect(getDamage)


Its a bit messy and thanks. The problem is as stated from the title. (The Attack function.) It will somehow store the number of times where the code is executed in the sabre and will just repeatedly be fired when touched multiple times when touched. Thanks!

3 comments

0

This is not the full code.

0

I found out that the "Touched" function will stacks after every hit, when it does not mean the nil criteria.

0

The script will yield and stacks when it does not touch anything, can anyone help me with this?

1 answer

✓ Accepted answer

Use a debounce.

local debounce = false
Event:Connect(function()
	if not debounce then
		debounce = true
		-- Do your stuff
		wait(10)
		debounce = false
	end
end)

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