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

Why does my for loop keep adding a value when the statement has been met?

I'm trying to make a kill confirmed script however when I walk into the killtag it adds 1 more times than it should even though I have a debounce inserted

for i,v in pairs(workspace:GetChildren())do
			if v.Name == "KillTag" and not Debounce[ v ] then
				v.PrimaryPart.Touched:Connect(function(child)
					local Hum = child.Parent:FindFirstChildOfClass("Humanoid") or child.Parent.Parent:FindFirstChildOfClass("Humanoid") 
					if game.Players[Hum.Parent.Name].TeamColor ~= v.PrimaryPart.BrickColor and Hum.Health > 0 then
						if v.PrimaryPart.BrickColor == BrickColor.new("Really blue") then 
								Debounce[ v ] = true
								v:Destroy()
								GameFolder.RedTeamScore.Value = GameFolder.RedTeamScore.Value+1 	
							elseif v.PrimaryPart.BrickColor == BrickColor.new("Really red") then
								Debounce[ v ] = true
								v:Destroy()
								GameFolder.BlueTeamScore.Value = GameFolder.BlueTeamScore.Value+1 							
							end
					elseif game.Players[Hum.Parent.Name].TeamColor == v.PrimaryPart.BrickColor and Hum.Health > 0 then
						if not Debounce[ v ] then
							Debounce[ v ] = true
							v:Destroy()
						end						
					end
				end)
			end
		end

1 comment

0

It's probably because you're not breaking the loop? So, where ever you want it to end, put break. I'm not 100% sure this will fix the issue.

2 answers

I don’t think you need that many debounces. I’m not familiar on debouncing but if everything’s in the kill if statement then you shouldn’t have to denounce everything since if the kill if statement is denounced nothing else will run inside it.

2 comments

0

I think what is happening is you aren’t denouncing the right thing. If that is a function that is being called by a touch connect event then you need to debounce the function call otherwise it will call it multiple times and check to see if it’s kill tag multiple times

0

If you release more code I can definitely find out the problem. It’s just hard to know what you are referencing.

your code is quite hard to read, but here is a remake of what i think your looking for:

locla debouce = false
for _, item in pairs(workspace:GetChildren()) do
	if item.Name:lower() == "killtag" then

		function OnTouched(part)
			if  debouce then return end
			local player game.Players:GetPlayerFromCharacter(part.Parent)
			local humanoid = part.Parent:FindFirstChild("Humanoid");
			
			if(humanoid and player) then
				if(item.BrickColor ~= player.TeamColor) then
					GivePoints(player)
				end
			end
		end

		item.PrimaryPart.Touched:Connect(OnTouched)
	end
end


function GivePoints(player)
	--if on red team, give to blue
	--if on blue team, give to red

	if(player.TeamColor == BrickColor.new("Really Red") then
		GameFolder.[Team name/color].Value = GameFolder.[Team name/color].Value+1
	else
		GameFolder.[Team name/color].Value = GameFolder.[Team name/color].Value+1
	end
end

sorry if i missed some of the essential part of the code, its b/c its difficult to read.

2 comments

0

This would solve my problem but in a way I would prefer not to do since this debounce would halt the script for the rest of the players as well and I'm somewhat reluctant to do that of fear from the player base

0

it wont -_-

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