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