Both an if and an else statement to run simultaneously problem

I wanted to make a spawning enemy system like Minecraft that spawn the monster around the player but not close to the player

I am new and I cant find tutorial or anything about this so I just try and this is my best one but I ran into a problem the code if statement that have else statement both run when the monster spawn in the area that the monster cant spawn is the red brick and the spawn area is the grey brick but when the monster spawn outside the red area the else statement did not run is there a mistake in my code please let me know or is there a better way of doing this and I also don’t think its a Roblox problem

here the code that I use

local SpawnArea = game.Workspace.MiddleSpawningPart.MonsterSpawnArea
local Moster = game.ServerStorage.MosterFolder.Monster
local MiddlePart = game.Workspace.MiddleSpawningPart.MainPart

local MosterGroupSize = math.random(1,5)
local MosterCount = 0
local timesrun = 0

print(MosterGroupSize, "is the group size")

while MosterCount < MosterGroupSize do
	timesrun = timesrun + 1
	print(timesrun, "times run")
	
	local minX, minY, minZ = SpawnArea.CFrame.Position.X - SpawnArea.Size.X / 2, SpawnArea.CFrame.Position.Y - SpawnArea.Size.Y / 2, SpawnArea.CFrame.Position.Z - SpawnArea.Size.Z / 2 -- get the minimum X, Y, and Z coordinates of the spawn area
	local maxX, maxY, maxZ = SpawnArea.CFrame.Position.X + SpawnArea.Size.X / 2, SpawnArea.CFrame.Position.Y + SpawnArea.Size.Y / 2, SpawnArea.CFrame.Position.Z + SpawnArea.Size.Z / 2 -- get the maximum X, Y, and Z coordinates of the spawn area


	
	local randomX = minX + (maxX - minX) * math.random() -- get a random X coordinate within the spawn area
	local randomY = minY + (maxY - minY) * math.random() -- get a random Y coordinate within the spawn area
	local randomZ = minZ + (maxZ - minZ) * math.random() -- get a random Z coordinate within the spawn area
	
	MosterClone = Moster:Clone() -- make a clone of the moster
	MosterClone:SetPrimaryPartCFrame(CFrame.new(randomX, randomY, randomZ)) -- set the position of the clone in a random place in the spawn area
	MosterClone.Parent = game.Workspace -- put the clone in the workspace
	
	local foundParts = game.Workspace:GetPartsInPart(MosterClone.MonsterHead)
	for Index, Value in pairs(foundParts) do
		
		if Value.Name ~= "MonsterCantSpawnArea" then
			MosterCount = MosterCount + 1
			print("moster spawn number ", MosterCount)
			
		else
			MosterClone:Destroy()
			print("destroy")
		end
		
	end
	
	task.wait()
end

this is after the code run and I got unlucky both the monster spawn in the red area

output
1 screenshot

any help would be great and thank for reading

I think you are running into a logic issue in their enemy spawning system — both the if and else code are being triggered because they’re looping over all foundParts, not breaking the loop once they know the monster is in an invalid area. Your code loops through every part inside the monster’s head (GetPartsInPart). If it finds a bad part (like the red “MonsterCantSpawnArea”), it destroys the monster. But if any other part is hit first, it counts the monster as valid and increments the counter before realizing it’s inside a bad area.

we gotta do these then:

  • assume the spawn is valid at first.
  • Loop through found parts, and mark it invalid if any part is "MonsterCantSpawnArea".
  • Only add the monster if it remained valid through the whole loop.
  • Break early from the loop if invalid.

Don’t process each part like it’s the final decision. Instead, check all parts first, then act once at the end based on whether anything bad was found.

try this one & let me know!

local SpawnArea = game.Workspace.MiddleSpawningPart.MonsterSpawnArea
local Monster = game.ServerStorage.MosterFolder.Monster
local MiddlePart = game.Workspace.MiddleSpawningPart.MainPart

local MonsterGroupSize = math.random(1, 5)
local MonsterCount = 0
local timesRun = 0

print(MonsterGroupSize, "is the group size")

while MonsterCount < MonsterGroupSize do
	timesRun += 1
	print(timesRun, "times run")

	local minX = SpawnArea.Position.X - SpawnArea.Size.X / 2
	local minY = SpawnArea.Position.Y - SpawnArea.Size.Y / 2
	local minZ = SpawnArea.Position.Z - SpawnArea.Size.Z / 2

	local maxX = SpawnArea.Position.X + SpawnArea.Size.X / 2
	local maxY = SpawnArea.Position.Y + SpawnArea.Size.Y / 2
	local maxZ = SpawnArea.Position.Z + SpawnArea.Size.Z / 2

	local randomX = math.random() * (maxX - minX) + minX
	local randomY = math.random() * (maxY - minY) + minY
	local randomZ = math.random() * (maxZ - minZ) + minZ

	local monsterClone = Monster:Clone()
	monsterClone:SetPrimaryPartCFrame(CFrame.new(randomX, randomY, randomZ))
	monsterClone.Parent = workspace

	local isValidSpawn = true
	local foundParts = workspace:GetPartsInPart(monsterClone.MonsterHead)

	for _, part in pairs(foundParts) do
		if part.Name == "MonsterCantSpawnArea" then
			isValidSpawn = false
			break
		end
	end

	if isValidSpawn then
		MonsterCount += 1
		print("monster spawn number ", MonsterCount)
	else
		print("Invalid spawn, destroying")
		monsterClone:Destroy()
	end

	task.wait()
end
1 Like