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

Why is this creating multiple IntValues when touched?

So i have this sword tool that has a attack script and when you touch a chicken it adds a intvalue but it creates a new value every time you touch the chicken i want it to just create one value even if you touch it multiple times

Heres the script

function Sword:Connect()
	Handle.Touched:connect(function(hit)
		local myPlayer = GLib.GetPlayerFromPart(Tool)
		local character, player, humanoid = GLib.GetCharacterFromPart(hit)
		
		if myPlayer~=nil and character~=nil and humanoid~=nil and myPlayer~=player then
			local isTeammate = GLib.IsTeammate(myPlayer, player)
			local myCharacter = myPlayer.Character
			local myHumanoid = myCharacter and myCharacter:FindFirstChild'Humanoid'
			
			if (Config.CanTeamkill.Value==true or isTeammate~=true) and (myHumanoid and myHumanoid:IsA'Humanoid' and myHumanoid.Health > 0) and (Config.CanKillWithForceField.Value or myCharacter:FindFirstChild'ForceField'==nil) then
				local doDamage = Config.IdleDamage.Value
				if Sword.State == 'Slashing' then
					doDamage = Config.SlashDamage.Value
				elseif Sword.State == 'Lunging' then
					doDamage = Config.LungeDamage.Value
				end
				
				GLib.TagHumanoid(humanoid, myPlayer, 1)
				humanoid:TakeDamage(doDamage)
				
				-- The thing that i want debounced
				local dm = Instance.new("IntValue", humanoid)
				dm.Name = "DamageTaken"
				local pl = Instance.new("StringValue", dm)
				pl.Name = plr.Name
			end
		end
	end)
end

1 answer

Because the touched object is actually touched multiple times, although it may only look like once.

To fix this you can add a debounce.

Also, it's worth the mention that function Sword:Connect() will not work because function Sword is not an event, it's a function.

local debounce = false
local debounceTime = 3 -- Edit this to how you want it

function runDebounce()
	debounce = true
	wait(debounceTime)
	debounce = false
end

function Sword:Connect()
    Handle.Touched:connect(function(hit)
	if not debounce then
		runDebounce() --move this to wherever the debounce should be triggered
	        local myPlayer = GLib.GetPlayerFromPart(Tool)
	        local character, player, humanoid = GLib.GetCharacterFromPart(hit)
	
	        if myPlayer~=nil and character~=nil and humanoid~=nil and myPlayer~=player then
	            local isTeammate = GLib.IsTeammate(myPlayer, player)
	            local myCharacter = myPlayer.Character
	            local myHumanoid = myCharacter and myCharacter:FindFirstChild'Humanoid'
	
	            if (Config.CanTeamkill.Value==true or isTeammate~=true) and (myHumanoid and myHumanoid:IsA'Humanoid' and myHumanoid.Health > 0) and (Config.CanKillWithForceField.Value or myCharacter:FindFirstChild'ForceField'==nil) then
	                local doDamage = Config.IdleDamage.Value
	                if Sword.State == 'Slashing' then
	                    doDamage = Config.SlashDamage.Value
	                elseif Sword.State == 'Lunging' then
	                    doDamage = Config.LungeDamage.Value
	                end
	
	                GLib.TagHumanoid(humanoid, myPlayer, 1)
	                humanoid:TakeDamage(doDamage)
	
	                -- The thing that i want debounced
	                local dm = Instance.new("IntValue", humanoid)
	                dm.Name = "DamageTaken"
	                local pl = Instance.new("StringValue", dm)
	                pl.Name = plr.Name
	            end
	        end
		end
    end)
end

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