I have some difficulties about random object spawner
Code:
local RunService = game:GetService("RunService")
local Spawner = script.Parent
local Properties = Spawner.f_Properties -- folder
local Targets = Spawner.f_Targets -- folder
local PreviousCFrame
local CurrentCFrame
local p_TargetAmount = Properties.TargetAmount
RunService.Heartbeat:Connect(function()
PreviousCFrame = CurrentCFrame
CurrentCFrame = Spawner.CFrame
if (CurrentCFrame ~= PreviousCFrame) then
for i, v in pairs(Targets:GetChildren()) do
v:Destroy()
end
end
if (CheckTargetAmount() < p_TargetAmount.Value) then
for i = 1, (p_TargetAmount.Value - CheckTargetAmount()) do
local Target = Instance.new("Part", Targets)
Target.Name = "Target"
Target.Size = Vector3.new(2, 2, 2)
Target.Anchored = true
Target.Shape = Enum.PartType.Ball
Target.Position = Vector3.new(math.random(Spawner.Position.X - Spawner.Size.X/2, Spawner.Position.X + Spawner.Size.X/2), math.random(Spawner.Position.Y - Spawner.Size.Y/2, Spawner.Position.Y + Spawner.Size.Y/2), math.random(Spawner.Position.Z - Spawner.Size.Z/2, Spawner.Position.Z + Spawner.Size.Z/2))
end
end
end)
function CheckTargetAmount()
local Count = 0
for i, v in pairs(Targets:GetChildren()) do
Count += 1
end
return Count
end
The spawner is limited to its orientation. If the spawner is rotated, then the spawner will break. How can I make the objects spawn in the spawner accurately, even when the spawner is rotated?