Getting parts to spawn in a Region

Working on Coin generation system, this is my current script:

local Region = workspace.Region

local ServerStorage = game:GetService("ServerStorage")
local Coin = ServerStorage.Coin

local x = Region.Position.x
local z = Region.Position.Z

local coins = 0

while coins < 10 do
	local newcoin = Coin:Clone()
	newcoin.Parent = workspace
	local pos1 = math.random(x,z)
	local pos2 = math.random(x,z)
	newcoin.Position = Vector3.new(pos1,3,pos2)
	wait(3)
end

This just spawns the coins randomly between the numbers like 8 and 50, which is not what I want. I think my brain is just too tired. I was hoping their was an easier method, or what is more practical to use. Any ideas what I could do to get the coins to spawn in a region (another part)

6 Likes

You need to incorporate the size of the region into your code. Put this at the start:

xS = Region.Size.X/2
xZ = Region.Size.Z/2

And then replace the bit in the while with this:

local pos1 = math.random(x-xS,x+xS)
local pos2 = math.random(z-zS,z+zS)
10 Likes

Thanks you really saved me ! :slight_smile:

3 Likes