Hello Developers,
Recently I’ve made a spawning system which spawns a random item randomly inside a region3 on the X, Y axis but I need the item to spawn on top of surfaces and not just on the floor. I’ve tried using .Touched and Region3 but Touched wouldn’t register properly and Region3 kept and making the parts position move to the surface position but it would happen anywhere in the region not when the part is actually inside another one.
This is how I calculate the x and y but like I said I do not know how to calculate the y.
local X, Z = SpawnRegion.Size.X, SpawnRegion.Size.Z
local X, Z = math.random(1, X), math.random(1, Z)
local X, Z = (Spawn.Position - Vector3.new(Spawn.Size.X/2)) + Vector3.new(X, 0, 0), (Spawn.Position - Vector3.new(Spawn.Size.Z/2)) + Vector3.new(Z, 0, 0)
print(X)
Item.Parent = game.Workspace.ItemSpawns
Item.Anchored = true
local Items = Item:GetChildren()
for i, v in pairs(Items) do
if v:IsA("Part") then
v.Anchored = true
elseif v:IsA("UnionOperation") then
v.Anchored = true
end
end
local IgnoredPlayers = {}
for i, Player in pairs(game.Players:GetChildren()) do
if Player and Player.Character then
table.insert(IgnoredPlayers, Player.Character)
end
end
local PartsInWhitelist = game.Workspace:FindPartsInRegion3WithIgnoreList(SpawnRegion, IgnoredPlayers, 100)
Item.Position = Vector3.new(X.X, Spawn.Position + Vector3.new(Spawn.Size.Y/2), Z.Z)
Full script (Region3 for Y axis):
--[Variables]--
local SpawnSettings = require(script.SpawnSettings)
local Spawns = SpawnSettings.Spawns
local Spawn = script.Parent
local SpawnCorner1 = Spawn.Position - Vector3.new(Spawn.Size.X /2, Spawn.Size.Y/2, Spawn.Size.Z/2)
local SpawnCorner2 = Spawn.Position + Vector3.new(Spawn.Size.X /2, Spawn.Size.Y/2, Spawn.Size.Z/2)
local SpawnRegion = Region3.new(SpawnCorner1, SpawnCorner2)
local ServerStorage = game:GetService("ServerStorage")
local function SpawnItem()
local ItemTypeNumber = 0
for i, v in pairs(SpawnSettings.Items) do
ItemTypeNumber = ItemTypeNumber +1
end
ItemTypeNumber = math.random(1, ItemTypeNumber)
local ItemType = SpawnSettings.Items[ItemTypeNumber]
ServerStorage:WaitForChild(ItemType)
local ItemNumber = 0
for i, v in pairs(SpawnSettings.Spawns[ItemType]) do
ItemNumber = ItemNumber +1
end
print(ItemType)
local ItemNumber = math.random(1, ItemNumber)
local ItemName = SpawnSettings.Spawns[ItemType][ItemNumber]
--ANYTHING ABOVE IS NOT IMPORTANT TO THE QUESTION--
--ANYTHING ABOVE IS NOT IMPORTANT TO THE QUESTION--
--ANYTHING ABOVE IS NOT IMPORTANT TO THE QUESTION--
local Item = ServerStorage:WaitForChild(ItemType):WaitForChild(ItemName):Clone()
local X, Z = SpawnRegion.Size.X, SpawnRegion.Size.Z
local X, Z = math.random(1, X), math.random(1, Z)
local X, Z = (Spawn.Position - Vector3.new(Spawn.Size.X/2)) + Vector3.new(X, 0, 0), (Spawn.Position - Vector3.new(Spawn.Size.Z/2)) + Vector3.new(Z, 0, 0)
print(X)
Item.Parent = game.Workspace.ItemSpawns
Item.Anchored = true
local Items = Item:GetChildren()
for i, v in pairs(Items) do
if v:IsA("Part") then
v.Anchored = true
elseif v:IsA("UnionOperation") then
v.Anchored = true
end
end
local IgnoredPlayers = {}
for i, Player in pairs(game.Players:GetChildren()) do
if Player and Player.Character then
table.insert(IgnoredPlayers, Player.Character)
end
end
table.insert(IgnoredPlayers, Spawn)
local PartsInWhitelist = game.Workspace:FindPartsInRegion3WithIgnoreList(SpawnRegion, IgnoredPlayers, 100)
Item.Position = Vector3.new(X.X, Spawn.Position + Vector3.new(Spawn.Size.Y/2), Z.Z)
wait()
for i, Part in pairs(PartsInWhitelist) do
local PartCorner1 = Part.Position - Vector3.new(Part.Size.X /2, Part.Size.Y/2, Part.Size.Z/2)
local PartCorner2 = Part.Position + Vector3.new(Part.Size.X /2, Part.Size.Y/2, Part.Size.Z/2)
local PartRegion = Region3.new(PartCorner1, PartCorner2)
table.insert(IgnoredPlayers, Part)
local PartsInWhitelistParts = game.Workspace:FindPartsInRegion3WithIgnoreList(PartRegion, IgnoredPlayers, 1)
local p = Instance.new("Part", workspace)
p.Anchored = true
p.Transparency = 0.6
p.Size = PartRegion.Size
p.CFrame = PartRegion.CFrame
if PartsInWhitelistParts ~= nil then
for i, v in pairs(PartsInWhitelistParts) do
local Y = Part.Position - Vector3.new(Spawn.Size.Y/2)
Item.Position = Vector3.new(X.X, Y.Y, Z.Z)
end
end
end
end
SpawnItem()
This is what I’m trying to do.