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
any help would be great and thank for reading