If statement triggering without argument being true

So I’m trying to create a script that will detect if a player is already afflicted with a status effect, and to do this I’m trying to create Bool Values with names labeling what status the person has. The script however, is reading through the first if statement and running the code under it, rather than any of the else statements. I’m not even worried about if the Bool Values will work, I’m just confused on how the script is finding children that dont exist. Here is the code:

ScripterAttack.OnServerEvent:Connect(function(player, Pos, info)

		if info == "Malware" then 
			local Effects = Instance.new("Folder",workspace)
			Effects.Name = player.Name.." Effects"

			local Formula89Folder = Meshes:WaitForChild("Formula 89")
			local Hitbox = Formula89Folder:WaitForChild("Formula89Hitbox"):Clone()
			Hitbox.Position = Pos
			Hitbox.CFrame = Hitbox.CFrame * CFrame.new(0,5,0)
			Hitbox.Transparency = 0.5
			Hitbox.Parent = Effects
			local FailSafe = Formula89Folder:WaitForChild("FailSafe"):Clone()
			FailSafe.Parent = Effects

			Hitbox.Touched:Connect(function(hit)
				if debounce == false then 
					debounce = true
					local Enemy = hit.Parent
					if Enemy:GetChildren("AdInfected" or "SpyInfected" or "RansomInfected") then 
						print"First"
					elseif  Enemy:GetChildren("MalInfected") then 
						print("Infected")
					else
						local MalSkullGUI = RP.MalSkullClosed:Clone()
						MalSkullGUI.Parent = Enemy

						local MalInfected = Instance.new("BoolValue")
						MalInfected.Name = "MalInfected"
						MalInfected.Parent = Enemy

						print"Reading"
						task.wait(2)
						debounce = false
						Hitbox:Destroy()
					end
				end
			end)
		end
	end)```

No

Definitely no.

GetChildren doesn’t take any arguments. Currently, what you are doing is equivalent to Enemy:GetChildren(), which will always return an array and hence will always trigger the if statement.


Instead, you should do

if Enemy:FindFirstChild("AdInfected") or Enemy:FindFirstChild("SpyInfected") or Enemy:FindFirstChild("RansomInfected") then
    print("First")
elseif Enemy:FindFirstChild("MalInfected") then
    print("Infected")
else
    ... -- other code here
end

That worked tysm! Would I always use :FindFirstChild() when searching for specific children?

Yeah.

When checking if a child exists, it’s better to do :FindFirstChild() than PARENT.CHILD as well, as FindFirstChild will return nil if it doesn’t exist, while PARENT.CHILD will error

1 Like

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