Parts that clone in the Walls

Hello. I am pretty new to scripting in roblox so I hope that this isn’t a redundant question.

I want the parts to clone anywhere besides in the walls. I already have a script working so that the parts will spawn in the play area.

local Part = game.ReplicatedStorage.Part
local spawner = game.Workspace.Baseplate
Yheight = 2
TotalParts = 3

local function GetRandomPos(position, Xsize, Zsize, Yheight) -- gets the random
local randomX = math.random(-Xsize/2, Xsize/2)
local randomZ = math.random(-Zsize/2, Zsize/2)
local spawnAt = Vector3.new(randomX, Yheight, randomZ)
return spawnAt + position

end

while TotalParts > 0 do -- spawns 3 parts
local ClonePart = Part:Clone()
local randomPos = GetRandomPos(spawner.Position, spawner.Size.X, spawner.Size.Z, Yheight)
ClonePart.Parent = workspace
ClonePart.Position = randomPos
TotalParts -= 1
wait(0.01)
end

(the script will do this)

And what I want the objects to spawn anywhere NOT in the walls

Example:

Right now, there is a chance that the parts will spawn inside the walls, which is not what I want.

I read in https://education.roblox.com/en-us/resources/arcade-game-spawning-enemies of making a “safe area” within the game, so I attempted to modify my script with the walls:


local Part = game.ReplicatedStorage.Part
local spawner = game.Workspace.Baseplate
local Walls = game.Workspace.Walls.Position -- this here is most likely giving me issues 
-- I also tried local Walls = game.Workspace.Walls , which also showed an error
Yheight = 2

local function GetRandomPos(position, Xsize, Zsize, Yheight) -- gets the random
local randomX = math.random(-Xsize/2 - Walls, Xsize/2 + Walls)
local randomZ = math.random(-Zsize/2 - Walls, Zsize/2 + Walls)
local spawnAt = Vector3.new(randomX, Yheight, randomZ)

return spawnAt + position
end

This comes up with an error ^

I am not sure how to translate the wall position into the script, but I figured it wouldn’t work anyways. I tested before with making the spawn area a unified, separated spawn parts and it seems that it would average out the entirety of the size of the wall.

So I attempted to make a touch function that looks like this:

local Part = game.ReplicatedStorage.Part
local spawner = game.Workspace.Baseplate
local Walls = game.Workspace.Walls
Yheight = 2
TotalParts = 3

local function GetRandomPos(position, Xsize, Zsize, Yheight) -- gets the random
local randomX = math.random(-Xsize/2, Xsize/2)
local randomZ = math.random(-Zsize/2, Zsize/2)
local spawnAt = Vector3.new(randomX, Yheight, randomZ)
return spawnAt + position
end

local function WallTouch(ClonePart)
local ClonePart = Part:Clone()
if ClonePart then
ClonePart:Destroy()
local randomPos = GetRandomPos(spawner.Position, spawner.Size.X, spawner.Size.Z, Yheight)
ClonePart.Parent = workspace
ClonePart.Position = randomPos
end
end

while TotalParts > 0 do -- spawns 3 parts
local ClonePart = Part:Clone()
local randomPos = GetRandomPos(spawner.Position, spawner.Size.X, spawner.Size.Z, Yheight)
ClonePart.Parent = workspace
ClonePart.Position = randomPos
Walls.Touched:Connect(WallTouch(ClonePart))
WallTouch()
TotalParts -= 1
wait(0.01)
end

The error I got was “Touched is not a valid member of Model “Workspace.Walls”

The intended idea was that I would make it so that if the part touched the wall, then it would destroy itself and make another one instead until it cloned an object that didn’t touch the walls, although there might be a better way of doing this.

EDIT: I have resolved the issue, no more help is needed

Have you tried using BasePart | Roblox Creator Documentation? If that doesn’t work for you then try Region3. Also the error came up because you can’t use .Touched on a model. Same with :GetTouchingParts.

2 Likes

you might use a get touching parts then loop until it doesnt touch.

1 Like

I tried:

local Part = game.ReplicatedStorage.Part
local spawner = game.Workspace.Baseplate
local Walls = game.Workspace.Part
Yheight = 2
TotalParts = 3

local function GetRandomPos(position, Xsize, Zsize, Yheight) -- gets the random
	local randomX = math.random(-Xsize/2, Xsize/2)
	local randomZ = math.random(-Zsize/2, Zsize/2)
	local spawnAt = Vector3.new(randomX, Yheight, randomZ)
	
	return spawnAt + position
end

while TotalParts > 0 do -- spawns 3 parts
	local ClonePart = Part:Clone()
	local randomPos = GetRandomPos(spawner.Position, spawner.Size.X, spawner.Size.Z, Yheight)
	ClonePart.Parent = workspace
	ClonePart.Position = randomPos
	while ClonePart:GetTouchingParts(Walls) do
		ClonePart:Destroy()
		ClonePart.Parent = workspace -- here is where the error occurred
		ClonePart.Position = randomPos
	end
	TotalParts -= 1
	wait(0.01)
end

the error I got was
“The Parent property of Part is locked, current parent: NULL, new parent Workspace”

any help with that?
I changed the wall to a part instead of a model now

The problem is with this segment of code, you are destroying the part the immediately trying to set its parent and position (Part is destroyed so it will error).

while TotalParts > 0 do -- spawns 3 parts
	local ClonePart = Part:Clone()
	local randomPos = GetRandomPos(spawner.Position, spawner.Size.X, spawner.Size.Z, Yheight)
	ClonePart.Parent = workspace
	ClonePart.Position = randomPos
	while ClonePart:GetTouchingParts(Walls) do
		ClonePart:Destroy() -- you are destroying the part here
		ClonePart.Parent = workspace 
		ClonePart.Position = randomPos
	end
	TotalParts -= 1
	wait(0.01)
end

You could get away by just removing the :Destroy() and .Parent lines probably.

1 Like

I tried:

while TotalParts > 0 do -- spawns 3 parts
	local ClonePart = Part:Clone()
	local randomPos = GetRandomPos(spawner.Position, spawner.Size.X, spawner.Size.Z, Yheight)
	ClonePart.Parent = workspace
	ClonePart.Position = randomPos
	while ClonePart:GetTouchingParts(Walls) do
		ClonePart.Position = randomPos
		wait(1)
	end
	TotalParts -= 1
	wait(0.01)
end

it only seems to spawn 1 of the 3 pieces, and sometimes 0

I tried:

local Part = game.ReplicatedStorage.Part
local spawner = game.Workspace.Baseplate
local Walls = game.Workspace.Walls
local Size = Walls.Size
Yheight = 2
TotalParts = 3


local function GetRandomPos(position, Xsize, Zsize, Yheight) -- gets the random
	local randomX = math.random(-Xsize/2, Xsize/2)
	local randomZ = math.random(-Zsize/2, Zsize/2)
	local spawnAt = Vector3.new(randomX, Yheight, randomZ)
	
	return spawnAt + position
end

while TotalParts > 0 do -- spawns 3 parts
	local ClonePart = Part:Clone()
	local randomPos = GetRandomPos(spawner.Position, spawner.Size.X, spawner.Size.Z, Yheight)
	local touching
	ClonePart.Parent = workspace
	ClonePart.Position = randomPos
	ClonePart.Touched:connect(function(touching) --// When the part is touched, call
		local touching = ClonePart:GetTouchingParts(Walls) --// Get the table of touching parts.
		for _, p in next, touching do
			print(p.Name..' is touching '..ClonePart.Name..'!')
		end
		while ClonePart:GetTouchingParts() == Walls do
			local randomPos = GetRandomPos(spawner.Position, spawner.Size.X, spawner.Size.Z, Yheight)
			ClonePart.Position = randomPos
			print("teleporting...")
			wait(0.5)
		end
	end)
	TotalParts -= 1
	wait(0.01)
end

Script will run, but the parts still get stuck in the wall. I suspect that I did something wrong here

while ClonePart:GetTouchingParts() == Walls do
			local randomPos = GetRandomPos(spawner.Position, spawner.Size.X, spawner.Size.Z, Yheight)
			ClonePart.Position = randomPos
			print("teleporting...")
			wait(0.5)
		end

It detects that it is touching the baseplate and the walls however, just wont teleport it again.

Walls touching

nonono, i mean using repeat and break the loop when it didnt hit!

Try:

repeat
--spawn a part, i will call it a block
local touchedblock = block:GetTouchingParts()
if #touchedblock == 0 then
--if there is no part touching, we will break the loop now...
else
--if there is a part touching.. we will spawn a new part and retry again
block:Destroy()
break
end
wait()
until false

--after the loop it will have a part that didnt touch a wall