If and statement keeps running if only one condition is met

I’m trying to make a killstreak-kind of glove in my slap battles recreation by coding in all the phases and assigning particles to the players character, however every time a new phase is reached it works properly but every time I get a kill after it keeps duplicating the particle emitters when only one condition was met in my if and statement.

Screenshot of particles being duplicated (there should only be 1 aura, 1 aura2, and 1 aura3)

image

I’ve tried multiple solutions by adding a value in the character and looked at dev forum posts but none of them worked which is why I’m creating this topic.

The full script will be provided

Code: (FYI only phases 5 - 25 are added in)

-- GO TO LINE 78 FOR THE SECTION THAT DOESN'T WORK
local tool = script.Parent.Parent

local char = tool.Parent
local plr = game.Players:GetPlayerFromCharacter(char)
local anim = script:WaitForChild("slap")
local animT

local hitChars = {}
local d = false

local slapping = false

tool.Activated:Connect(function()
	if d then
		return print("debounce")
	end
	d = true
	local h = tool.Parent.Humanoid
	if not animT and not slapping then
		animT = h:LoadAnimation(anim)
		slapping = true
	end
	animT:Play()
	wait(script.Cooldown.Value)
	animT = nil
	slapping = false
	d = false
end)

tool.hitbox.Touched:Connect(function(hit)
	if hitChars[hit.Parent] or not d then
		return
	end
	if hit.Parent:FindFirstChild("Humanoid") then
		local ec = hit.Parent
		local plrc = tool.Parent
		local ehr = ec:FindFirstChild("HumanoidRootPart")
		local plrhr = plrc:FindFirstChild("HumanoidRootPart")
		if ec:FindFirstChild("protected") then
			return
		end
		if plrhr and ehr then
			script.Disabled = true
			ec.Humanoid.PlatformStand = false
			local plr = game.Players:GetPlayerFromCharacter(tool.Parent)
			plr.leaderstats.Slaps.Value = plr.leaderstats.Slaps.Value + 1
			ec.Humanoid.Sit = true
			local f = Instance.new("BodyVelocity", ehr)
			f.MaxForce = Vector3.new(2,2,2) * math.huge
			local dir = (ehr.CFrame.Position - plrhr.CFrame.Position).Unit
			f.Velocity = (dir + Vector3.new(0,1,0)).Unit * script.Power.Value
			local rot = Instance.new("BodyAngularVelocity", ehr)
			rot.AngularVelocity = Vector3.new(1,1,1) * math.pi * 1
			rot.MaxTorque = Vector3.new(2,2,2) * math.huge
			rot.P = 5000
			tool.Handle.slap:Play()
			local plrHit = game.ServerStorage:WaitForChild("PlrHit")
			if plrHit then
				if ec:FindFirstChild("PlrHit") then
				else
					local cplrHit = plrHit:Clone()
					cplrHit.Value = plrc.Name
					cplrHit.Parent = ec
				end
			end
			wait(0.35)
			f:Destroy()
			rot:Destroy()
			ec.Humanoid.Sit = false
		end
		hitChars[hit.Parent] = true
		wait(script.Stun.Value)
		script.Disabled = false
		hitChars[hit.Parent] = nil
	end
end)
-- SECTION THAT DOESN'T WORK - v
local five = false
local ten = false
local twenty5 = false
local fifty = false
local seventy5 = false
local hundred = false
local particles = false
script.Kills.Changed:Connect(function(value)
	if char.Head:FindFirstChild("NameUI") then
		char.Head:FindFirstChild("NameUI").TextLabel.Text = tostring(value)
	end
	char.Humanoid.WalkSpeed = char.Humanoid.WalkSpeed + 1
	script.Power.Value = script.Power.Value + 2
	if script.Kills.Value >= 5 and script.Kills.Value <= 9 and five == false then
		script.Parent.Attachment.FireParticle.Enabled = true
		if char:FindFirstChild("Phase") == nil then
			local phase = Instance.new("NumberValue", char)
			phase.Name = "Phase"
			phase.Value = 5
		end
		if char.Phase.Value == 5 and particles == false then
			local pclone = game.ServerStorage:WaitForChild("Phase5"):Clone()
			for i,v in pairs(pclone.Head:GetChildren()) do
				v.Parent = char.Head
			end
			for i,v in pairs(pclone.Torso:GetChildren()) do
				v.Parent = char.Torso
			end
			for i,v in pairs(pclone.LArm:GetChildren()) do
				v.Parent = char["Left Arm"]
			end
			for i,v in pairs(pclone.RArm:GetChildren()) do
				v.Parent = char["Right Arm"]
			end
			for i,v in pairs(pclone.LLeg:GetChildren()) do
				v.Parent = char["Left Leg"]
			end
			for i,v in pairs(pclone.RLeg:GetChildren()) do
				v.Parent = char["Right Leg"]
			end
			pclone:Destroy()
			particles = true
		end
		five = true
	elseif script.Kills.Value >= 10 and script.Kills.Value <= 24 and ten == false then
		script.Parent.Attachment.Fire10.Enabled = true
		char.Humanoid.WalkSpeed = 28
		script.Power.Value = 75
		if char.Phase.Value == 5 then
			for i,v in pairs(char:GetChildren()) do
				if v:IsA("BasePart") then
					for i,v in pairs(v:GetChildren()) do
						if v:IsA("ParticleEmitter") or v.Name == "Head" or v.Name == "Root" then
							v:Destroy()
						end
					end
				end
			end
			particles = false
			char.Phase.Value = 10
		end
		if char.Phase.Value == 10 and particles == false then
			local pclone = game.ServerStorage:WaitForChild("Phase10"):Clone()
			for i,v in pairs(pclone.Head:GetChildren()) do
				v.Parent = char.Head
			end
			for i,v in pairs(pclone.Torso:GetChildren()) do
				v.Parent = char.Torso
			end
			for i,v in pairs(pclone.LArm:GetChildren()) do
				v.Parent = char["Left Arm"]
			end
			for i,v in pairs(pclone.RArm:GetChildren()) do
				v.Parent = char["Right Arm"]
			end
			for i,v in pairs(pclone.LLeg:GetChildren()) do
				v.Parent = char["Left Leg"]
			end
			for i,v in pairs(pclone.RLeg:GetChildren()) do
				v.Parent = char["Right Leg"]
			end
			pclone.Part.Head.Parent = char.Head
			pclone.Part.Root.Parent = char.HumanoidRootPart
			pclone:Destroy()
			particles = true
		end
		ten = true
	elseif script.Kills.Value >= 25 and script.Kills.Value <= 49 and twenty5 == false then
		script.Parent.Attachment.Fire25.Enabled = true
		char.Humanoid.WalkSpeed = 3
		local pro = game.ServerStorage:WaitForChild("protected"):Clone()
		pro.Parent = char
		if char.Phase.Value == 10 then
			for i,v in pairs(char:GetChildren()) do
				if v:IsA("BasePart") then
					for i,v in pairs(v:GetChildren()) do
						if v:IsA("ParticleEmitter") or v.Name == "Head" or v.Name == "Root" then
							v:Destroy()
						end
					end
				end
			end
			particles = false
			char.Phase.Value = 25
		end
		wait(10)
		if char.Phase.Value == 25 and particles == false and char:FindFirstChild("Phase25") == false then
			local pclone = game.ServerStorage:WaitForChild("Phase25"):Clone()
			for i,v in pairs(pclone.Head:GetChildren()) do
				v.Parent = char.Head
			end
			for i,v in pairs(pclone.Torso:GetChildren()) do
				v.Parent = char.Torso
			end
			for i,v in pairs(pclone.LArm:GetChildren()) do
				v.Parent = char["Left Arm"]
			end
			for i,v in pairs(pclone.RArm:GetChildren()) do
				v.Parent = char["Right Arm"]
			end
			for i,v in pairs(pclone.LLeg:GetChildren()) do
				v.Parent = char["Left Leg"]
			end
			for i,v in pairs(pclone.RLeg:GetChildren()) do
				v.Parent = char["Right Leg"]
			end
			pclone:Destroy()
			particles = true
		end
		char.Humanoid.WalkSpeed = 38
		script.Power.Value = 100
		twenty5 = true
	elseif script.Kills.Value >= 50 and script.Kills.Value <= 74 and fifty == false then
		fifty = true
		script.Parent.Attachment.Fire50.Enabled = true
		char.Humanoid.WalkSpeed = 50
		script.Power.Value = 350
	elseif script.Kills.Value >= 75 and script.Kills.Value <= 99 and seventy5 == false then
		seventy5 = true
		script.Parent.Attachment.Fire75.Enabled = true
		char.Humanoid.WalkSpeed = 28
		script.Power.Value = 45
	elseif script.Kills.Value >= 100 and hundred == false then
		hundred = true
		script.Parent.Attachment.Fire100.Enabled = true
		char.Humanoid.WalkSpeed = 100
		script.Power.Value = 500
	end
end)

Thanks for checking out this post and considering a solution if you’re replying!

Which if condition is the problematic one?

This one and the other ones that are similar to this

if char.Phase.Value == 5 and particles == false then
			local pclone = game.ServerStorage:WaitForChild("Phase5"):Clone()
			for i,v in pairs(pclone.Head:GetChildren()) do
				v.Parent = char.Head
			end
			for i,v in pairs(pclone.Torso:GetChildren()) do
				v.Parent = char.Torso
			end
			for i,v in pairs(pclone.LArm:GetChildren()) do
				v.Parent = char["Left Arm"]
			end
			for i,v in pairs(pclone.RArm:GetChildren()) do
				v.Parent = char["Right Arm"]
			end
			for i,v in pairs(pclone.LLeg:GetChildren()) do
				v.Parent = char["Left Leg"]
			end
			for i,v in pairs(pclone.RLeg:GetChildren()) do
				v.Parent = char["Right Leg"]
			end
			pclone:Destroy()
			particles = true
		end

Anyone else able to help with this?

Are you sure particles == false?

particles is set to false when the script starts and is set to false every new phase

Sorry for bringing the topic back up to the list again but can somebody please find a solution for this soon? I really want to get this glove back in soon.

And can somebody try finding a solution soon? I’m sure variables are set correctly

Im not sure why this isnt working. is it a localscript or a serverscript? Im assuming serverscript

1 Like

It’s a server script of course because it calls :FireAllClients() for a remote event but thats not the important issue

Try adding brackets.

if (char.Phase.Value == 5 and particles == false) then

particles are still being duplicated but ill try adding them to previous if statements
edit: still being duplicated

Hello, If you still want help, here it is:

so, the issue seems to come from the fact that you’re not resetting the ‘particles’ boolean variable after you reach a new phase. As a result, when the player progresses to the next phase, your script still thinks that the particles for the current phase are present, which is causing the duplication. you should reset the ‘particles’ boolean to false when a new phase starts, right after you set the phase boolean (like ‘five’, ‘ten’, ‘twenty5’, etc.) to true. Here’s how you can do that for phase 5:

five = true
particles = false

Do this for all phases. Hope it helps :slight_smile:

So the issue is that there are multiple auras when there should only be 1 or is it something else that I missed?

If thats the case it shouldnt be pairs loops, put all the auras in a folder in replicated or serverstorage, here im doing replicated storage. You can make subfolders for the different stages as well. So

local c1 = game.ReplicatedStorage.Auras.Phase5.HeadAura:Clone()
c1.Parent = char.Head

and so on for each part. if this doesnt fix it, you need to make sure to remove all auras when the phase is changed. Im assuming phase is different kills so phase 1 is 10 kills, phase 2 is 25 kills, etc.

so

char.Phase:GetPropertyChangedSignal("Value"):Connect(function() -- I think theres a better way to detect changes so if you find that i recommend you use it
    for i, v in ipairs(char:GetDescendants()) do
        if v:IsA("ParticleEmitter") then
            v:Destroy()
        end
    end
end)

Hopefully this should work, if im understanding it correctly

still being duplicated.
i put them right at the start of the if statements that cause new phases but yet its still being duplicated

script.Kills.Changed:Connect(function(value)
	if char.Head:FindFirstChild("NameUI") then
		char.Head:FindFirstChild("NameUI").TextLabel.Text = tostring(value)
	end
	char.Humanoid.WalkSpeed = char.Humanoid.WalkSpeed + 1
	script.Power.Value = script.Power.Value + 2
	if (script.Kills.Value >= 5 and script.Kills.Value <= 9 and five == false) then
		five = true
		particles = false
		script.Parent.Attachment.FireParticle.Enabled = true
		if char:FindFirstChild("Phase") == nil then
			local phase = Instance.new("NumberValue", char)
			phase.Name = "Phase"
			phase.Value = 5
		end
		if (char.Phase.Value == 5 and particles == false) then
			local pclone = game.ServerStorage:WaitForChild("Phase5"):Clone()
			for i,v in pairs(pclone.Head:GetChildren()) do
				v.Parent = char.Head
			end
			for i,v in pairs(pclone.Torso:GetChildren()) do
				v.Parent = char.Torso
			end
			for i,v in pairs(pclone.LArm:GetChildren()) do
				v.Parent = char["Left Arm"]
			end
			for i,v in pairs(pclone.RArm:GetChildren()) do
				v.Parent = char["Right Arm"]
			end
			for i,v in pairs(pclone.LLeg:GetChildren()) do
				v.Parent = char["Left Leg"]
			end
			for i,v in pairs(pclone.RLeg:GetChildren()) do
				v.Parent = char["Right Leg"]
			end
			pclone:Destroy()
			particles = true
		end
	elseif (script.Kills.Value >= 10 and script.Kills.Value <= 24 and ten == false) then
		ten = true
		particles = false
		script.Parent.Attachment.Fire10.Enabled = true
		char.Humanoid.WalkSpeed = 28
		script.Power.Value = 75
		if char.Phase.Value == 5 then
			for i,v in pairs(char:GetChildren()) do
				if v:IsA("BasePart") then
					for i,v in pairs(v:GetChildren()) do
						if v:IsA("ParticleEmitter") or v.Name == "Head" or v.Name == "Root" then
							v:Destroy()
						end
					end
				end
			end
			particles = false
			char.Phase.Value = 10
		end
		if (char.Phase.Value == 10 and particles == false) then
			local pclone = game.ServerStorage:WaitForChild("Phase10"):Clone()
			for i,v in pairs(pclone.Head:GetChildren()) do
				v.Parent = char.Head
			end
			for i,v in pairs(pclone.Torso:GetChildren()) do
				v.Parent = char.Torso
			end
			for i,v in pairs(pclone.LArm:GetChildren()) do
				v.Parent = char["Left Arm"]
			end
			for i,v in pairs(pclone.RArm:GetChildren()) do
				v.Parent = char["Right Arm"]
			end
			for i,v in pairs(pclone.LLeg:GetChildren()) do
				v.Parent = char["Left Leg"]
			end
			for i,v in pairs(pclone.RLeg:GetChildren()) do
				v.Parent = char["Right Leg"]
			end
			pclone.Part.Head.Parent = char.Head
			pclone.Part.Root.Parent = char.HumanoidRootPart
			pclone:Destroy()
			particles = true
		end
	elseif (script.Kills.Value >= 25 and script.Kills.Value <= 49 and twenty5 == false) then
		twenty5 = true
		particles = false
		script.Parent.Attachment.Fire25.Enabled = true
		char.Humanoid.WalkSpeed = 3
		local pro = game.ServerStorage:WaitForChild("protected"):Clone()
		pro.Parent = char
		if char.Phase.Value == 10 then
			for i,v in pairs(char:GetChildren()) do
				if v:IsA("BasePart") then
					for i,v in pairs(v:GetChildren()) do
						if v:IsA("ParticleEmitter") or v.Name == "Head" or v.Name == "Root" then
							v:Destroy()
						end
					end
				end
			end
			particles = false
			char.Phase.Value = 25
		end
		wait(10)
		if (char.Phase.Value == 25 and particles == false and char:FindFirstChild("Phase25")) == false then
			local pclone = game.ServerStorage:WaitForChild("Phase25"):Clone()
			for i,v in pairs(pclone.Head:GetChildren()) do
				v.Parent = char.Head
			end
			for i,v in pairs(pclone.Torso:GetChildren()) do
				v.Parent = char.Torso
			end
			for i,v in pairs(pclone.LArm:GetChildren()) do
				v.Parent = char["Left Arm"]
			end
			for i,v in pairs(pclone.RArm:GetChildren()) do
				v.Parent = char["Right Arm"]
			end
			for i,v in pairs(pclone.LLeg:GetChildren()) do
				v.Parent = char["Left Leg"]
			end
			for i,v in pairs(pclone.RLeg:GetChildren()) do
				v.Parent = char["Right Leg"]
			end
			pclone:Destroy()
			particles = true
		end
		char.Humanoid.WalkSpeed = 38
		script.Power.Value = 100
	elseif script.Kills.Value >= 50 and script.Kills.Value <= 74 and fifty == false then
		fifty = true
		script.Parent.Attachment.Fire50.Enabled = true
		char.Humanoid.WalkSpeed = 50
		script.Power.Value = 350
	elseif script.Kills.Value >= 75 and script.Kills.Value <= 99 and seventy5 == false then
		seventy5 = true
		script.Parent.Attachment.Fire75.Enabled = true
		char.Humanoid.WalkSpeed = 28
		script.Power.Value = 45
	elseif script.Kills.Value >= 100 and hundred == false then
		hundred = true
		script.Parent.Attachment.Fire100.Enabled = true
		char.Humanoid.WalkSpeed = 100
		script.Power.Value = 500
	end
end)

there’s multiple auras in each phase folder, each for every r6 body part
and removing auras works normally each phase, its just that particles keep getting duplicated each time you get a kill after a phase is triggered

and i also dont really understand ipairs or other pair functions either, im not the best at scripting

hmm okay try this solution:
Your code has many places where it might cause duplication of objects, or unexpected behavior, but without more context, it is difficult to say definitively what the problem is. However, here are some potential issues and solutions to them:

  1. Parenting: Be careful when changing the Parent of an object. In your code, you clone objects and change the Parent of these clones multiple times. This could potentially lead to duplications. For example, in the phase 5 block, you first set the parent of pclone.Head’s children to char.Head, then later you destroy pclone, which also destroys its children. But you have already set the parent of these children to char.Head, so they are not destroyed and remain in the workspace.
  2. Part Destroying: The code destroys all ParticleEmitters, and parts named “Head” or “Root” of the character’s body parts for each phase. Be careful about destroying parts of a character’s body, as this could lead to undesired consequences. Also, this destroying operation is done in each phase change which may duplicate the destroying operation for the same part.
  3. Conditional Checks: You have conditions like if (char.Phase.Value == 5 and particles == false) then inside a condition that sets particles = false. The inner condition will always be true if char.Phase.Value == 5 is true. This kind of redundancy might make your script confusing and prone to mistakes. Try to clean up and simplify your code by reducing redundant checks and improving the logic.
  4. Script Execution: There is a condition, if (script.Kills.Value >= 5 and script.Kills.Value <= 9 and five == false) then, which is checking whether the five flag is false, then sets it to true. If the script execution reaches this part of the code again, even if script.Kills.Value is still in range, it won’t execute this block of code. You may need to review this logic to make sure it’s doing what you intend.
  5. Global Variables: The boolean variables five, ten, twenty5, fifty, seventy5, hundred are global, and it seems like they control whether a phase has already been set. But if these variables are not initialized to false somewhere else before this script, they will be nil and could cause issues.

It’s recommended to simplify your code as much as possible and break it down into smaller functions to improve readability and maintainability. If these suggestions do not fix your problem, please provide more context or clarify your issue further

  1. pclone is destroyed so ServerStorage doesn’t fill up with clones
  2. “Head” and “Root” are attachments
  3. I removed other particles = false that are inside if statements that set it to false
  4. I’m pretty sure it’s supposed to be doing as it’s intended, after the phase is initialized since it’s still in range, but particles is true so it skips the block again, and more power and speed is added at the original function anyways
  5. Every phase variable and particles is local, did you read all of the code in the first post?

Sorry if I’m not really giving too much context here, this script is just wonky.

ok im gonna try to explain pairs loop,

for i, v
I is the number it loops through, the third time it loops, I would be 3 (probably)
v is the instance. if you were to loop through a table of children, so GetChildren(), V is the instance it is currently looping through. V can also mean value, which is what it stands for.

for i, v in pairs(thing:GetChildren())
That loops through every child of the parent, v being the instance it is currently looping through. It does this in a random order

for i, v in ipairs(thing:GetChildren())
adding an i before the pairs, making ipairs, makes it loop through it in a set order, from the start to end, instead of random. usually it doesn’t really matter but sometimes you should use this, most often with tables, and not GetChildren() or GetDescendants()

When you are using pairs in that pclone, you are looping through EVERY child of that head, leg, and other things. Im assuming you dont want that and you should add a filter to make sure its a particle emitter, with v:IsA(“ParticleEmitter”)

theres probably better explanations online if that wasn’t clear

thanks for the explanation and pclone’s childs only have particleemitters or attachments