How can I get Coins to spawn in Random Locations

You would parent the coin to the spawner script, use a variable to define it and then instead of instancing a part just clone the coin.

It would look fine if you want specific areas which spawn coins as opposed to an entire map with randomly spawned coins.


not really sure why but it does this

local randoms = math.random(-5, 5)
coinclone.Position += Vector3.new(randoms, randoms, randoms)

You’d need to change these two lines of code, to change how the coins are scattered.

Something like this might work better:

local spawncframes = {CFrame.new(699.21, 30.062, -5415.25), CFrame.new(684.67, 30.062, -5435.845)}
local rand = math.random(1, #spawncframes)
local coin = game.ServerStorage:WaitForChild("Coin")

while task.wait(10) do
	local coinclone = coin:Clone()
	local randoms1 = math.random(-15, 15)
	local randoms2 = math.random(-15, 15)
	local randoms3 = math.random(-15, 15)
	coinclone.CFrame = spawncframes[rand]
	coinclone.Position += Vector3.new(randoms1, randoms2, randoms3)
	coinclone.Parent = workspace
end

What part makes it go up? I don’t want it go up like this. image
I just want it to be at 0 for the height

If you’re referring to my code sample as “an entire map randomly spawned with coins”, I’d recommend looking into the API I linked. The module for zones that I use works in folders meaning you can create zones as large or as tiny as you want all in the same folder and a random point will only be generated inside of those zones.

As for your code though, indeed it would look fine once a user would change up the values.

Like this?

local ZoneModule = require(script:WaitForChild("Zone"))
local CoinZone = ZoneModule.new(game.Workspace:WaitForChild("CoinZone"))
local coin = game.ServerStorage:WaitForChild("Coin")

while task.wait(1) do
	local coinclone = coin:Clone()
end

Pretty sure it’s because of the Y change.

local spawncframes = {CFrame.new(699.21, 30.062, -5415.25), CFrame.new(684.67, 30.062, -5435.845)}
local rand = math.random(1, #spawncframes)
local coin = game.ServerStorage:WaitForChild("Coin")

while task.wait(10) do
	local coinclone = coin:Clone()
	local randoms1 = math.random(-15, 15)
	local randoms2 = 15
	local randoms3 = math.random(-15, 15)
	coinclone.CFrame = spawncframes[rand]
	coinclone.Position += Vector3.new(randoms1, randoms2, randoms3)
	coinclone.Parent = workspace
end

That should keep them all at the same position.

local ZoneModule = require(script:WaitForChild("Zone"))
local CoinZone = ZoneModule.new(game.Workspace:WaitForChild("CoinZone"))
local coin = game.ServerStorage:WaitForChild("Coin")

while task.wait(1) do
	local coinclone = coin:Clone()
        coinclone.Position = CoinZone:getRandomPoint()
        coinclone.Parent = game.Workspace
end

Okay, and is there a way to make it have a Max amount of Coins (spawn), and once it hits the max it wont spawn again till you collect a coin? If that makes sense

local spawncframes = {CFrame.new(699.21, 30.062, -5415.25), CFrame.new(684.67, 30.062, -5435.845)}
local rand = math.random(1, #spawncframes)
local coin = game.ServerStorage:WaitForChild("Coin")

while task.wait(10) do
	local coinclone = coin:Clone()
	local randoms1 = math.random(-15, 15)
	local randoms2 = math.random(-15, 15)
	local randoms3 = math.random(-15, 15)
	coinclone.CFrame = spawncframes[rand]
	coinclone.Position += Vector3.new(randoms1, 0, randoms3)
	coinclone.Parent = workspace
end

The Y component of Vector3 values causes a shift in the Y axis of a physical instance, here by setting it to 0 you retain the Y component of the original CFrame.

1 Like
local ZoneModule = require(script:WaitForChild("Zone"))
local CoinZone = ZoneModule.new(game.Workspace:WaitForChild("CoinZone"))
local coin = game.ServerStorage:WaitForChild("Coin")
local CoinAmount = 0
local MaxCoinAmount = 50

while task.wait(1) do
        if CoinAmount <= MaxCoinAmount then
        CoinAmount = CoinAmount + 1
	local coinclone = coin:Clone()
        coinclone.Position = CoinZone:getRandomPoint()
        coinclone.Parent = game.Workspace
        coinclone.Touched:Connect(function()
                CoinAmount = CoinAmount - 1
               coinclone:Destroy()
        end)
       end
end

Something like this.

2 Likes

Ah okay I understand, now just a few more questions, how can I do the Max Coin thingy, and how can I make it not on it’s side?

local spawncframes = {CFrame.new(0, 0, 0), CFrame.new(50, 50, 50)}
local rand = math.random(1, #spawncframes)
local coin = game.ServerStorage:WaitForChild("Coin")

while task.wait(10) do
	local coinclone = coin:Clone()
	local randoms1 = math.random(-15, 15)
	local randoms2 = math.random(-15, 15)
	local randoms3 = math.random(-15, 15)
	coinclone.CFrame = spawncframes[rand]
	coinclone.Position += Vector3.new(randoms1, 0, randoms3)
	coinclone.Orientation = Vector3.new(0, 0, 0)
	coinclone.Parent = workspace
end
5 Likes

Okay, I got it working! Thank you so much, I appreciate it!

1 Like

No problem, glad we got there in the end.

2 Likes