How can I get Coins to spawn in Random Locations

Hello! How can I make a Coin System where it spawns in different random Areas within a specific location? Just as an example, in Pet Simulator X It spawns Coins in random areas, that is pretty much what I am asking on how to do. How can I achieve this?

Any help will be appreciated!

2 Likes

The main way I can think to do this is:

  1. Putting a coin model or part in replicated storage
  2. Cloning the coin from a script that also sets the position, or primary part CFrame if a model, to a vector3.new value.
  3. You can make the coin spawn in a random location my randomizing the vector3 values. ( x,y, z )

Example:
local coinPosition = vector3.new( math.Random(-10,10),1,math.Random(-10,10) )

The Roblox Hub page for the math.Random ( Scroll down a bit ): math | Roblox Creator Documentation
The Roblox Hub page for Vector3: Vector3 | Roblox Creator Documentation

I have this Script I used in a game that I made about a year ago, but I don’t know how I did this, so I am kinda lost on how I got the positions

local coin = game.ServerStorage.Coin

while true do
    local clone = coin:Clone()
    clone.Parent = game.Workspace
    local randomizer = math.random(1,2)
    
    if randomizer == 1 then
        clone.Position = Vector3.new(math.random(-40, 155), 539.45, math.random(-172, 89))
    elseif randomizer == 2 then
        clone.Position = Vector3.new(math.random(-84, 126), 539.45, math.random(6, 184.247))
    else
        clone.Position = Vector3.new(math.random(-40, 155), 539.45, math.random(-172, 89))    -- Just in case it glitches out
    end
    
    wait(math.random(0.9, 3))  -- Or realy whatever value u want
end

Here is just a basic one:

local coin = game.ServerStorage.Coin

while wait(math.Random(3,5)) do
    local clone = coin:Clone()
    clone.Parent = game.Workspace
    clone.Position = Vector3.new(math.random(-40, 155), 539.45, math.random(-172, 89))
end

In your script you could do:

Coin.Position = Vector3.new(math.random(min, max), math.random(min, max), math.random(min, max))

which would position the coin randomly within the given parameters, though this may put the coin in places you don’t want it to be. So you may want to use something like Raycasting.

1 Like

Can you explain a bit better? I am not really catching what you’re talking about. How do I get the position I want it to go to? Also is there a way to have a max amount of coins that spawn?

Sure, what are you confused about specifically? Also, to make a max coin amount you could do a for i, v loop, but I don’t remember how to use them properly. You could do something like this:

local coin = game.ServerStorage.Coin
local totalCoinsSpawned = 0

while totalCoinsSpawned < 5 do -- This will run until 5 coins are spawned
    wait(3) -- every 3 seconds, the stuff below will happen
    totalCoinsSpawned = totalCoinsSpawned + 1 -- this adds up 1 to the current value of the totalCoinsSpawned, which is currently 0.

    local clone = coin:Clone() -- This takes the coin part and makes a copy of it
    clone.Parent = game.Workspace
    clone.Position = Vector3.new(math.random(-40, 155), 539.45, math.random(-172, 89)) -- This sets the cloned coins position to a random x and z value. ( y value will always be 539.45 )
end

Every part has a position, the code above sets the position to a random spot that you set.

1 Like

You could try something like this. This will spawn a part on the base position and then apply a random amount on the X, Z axis

local BASE_POS = script.Parent["YOUR PARTS / BASE POSITION"]


while true do
	local PA = Instance.new("Part")
	PA.Anchored = true
	PA.Position = Vector3.new(BASE_POS.X + math.random(-NUM, NUM), BASE_POS.Y, BASE_POS.Z + math.random(-NUM, NUM))
	PA.Parent = game.Workspace
	task.wait(1)
end

Okay so for the Max Coins thing, lets say you collect it, it wont respawn because it’s already at 5, how can I fix that? Plus, what I mean is what Position do I put in what spot, that is really confusing to me.

math.random() is the minimum value, then the maximum value. Ex:

math.random(5,10) -- 5 is the min, 10 is the max. Maybe another way to understand is this: math.random(min number, max number)

To fix the coins not respawning after 5 coins have already spawned, get rid of the 5 and put the “true”:

local coin = game.ServerStorage.Coin

while true do -- This will run forever
    wait(3) -- every 3 seconds, the stuff below will happen

    local clone = coin:Clone() -- This takes the coin part and makes a copy of it
    clone.Parent = game.Workspace
    clone.Position = Vector3.new(math.random(-40, 155), 539.45, math.random(-172, 89)) -- This sets the cloned coins position to a random x and z value. ( y value will always be 539.45 )
end

How do I get the positions I want it to spawn to within in? I don’t want a random position that isn’t where I don’t want it. Also sorry I just suck at positions, and I need a clear explanation on how to do it. (Also I have to get off so I can not respond anymore till tomorrow)

1 Like

you can set the position to a parts position by doing something like this:

local coin = game.ServerStorage.Coin

while true do -- This will run forever
    wait(3) -- every 3 seconds, the stuff below will happen

    local clone = coin:Clone() -- This takes the coin part and makes a copy of it
    clone.Parent = game.Workspace
    clone.Position = game.Workspace.part.Position -- this sets the position of the cloned coin to a part named "part" inside of Workspace. ( You could also add a vector3 to a position as a position is just another word for vector3. )
end

Here is a YouTube video on vector3s hope this helps: Roblox Beginner Scripting Tutorial: Episode #14 - Vector3.new - YouTube ( The video actually explains vector3s really well :slight_smile: )

Going back to this, for Min, and Max number what do you mean? Like let’s say a part is at 27, 5, 24, would you put 27, 24, that’s what I am confused about, and I am also confused on the max coin thingy, because I said once the 5 hit, you collect it, then it doesn’t respawn. I still want there to be the max which would be let’s say 5, but if you collect it, it will respawn.

the minimum value for that number and the maximum for that number. Lets say this: vector3.new( math.random(1,5), 3, 4 )

The math.random(1,5) is the x value. The 3 is the y value. The 4 is the z value.

The x value is going to be 1, 2, 3, 4, or 5 because those numbers are between 1 and 5 and the math.random is in the area where the x value is.

Vector3.new(x-value, y-value, z-value )

The x-value in this case is going to be between 1 and 5.

1 Like

I understand but what in the world am I supposed to do to get the position to change in this?! That’s basically all I am asking now also I am on my phone from starting forward I can not respond till tomorrow

clone.Position = Vector3.new(math.random(-40, 155), 539.45, math.random(-172, 89))
1 Like

every part has a position, same as vector3 value, in the properties menu. Make a part where you want a coin to spawn and look at the position. After this you can add vector3s to make it more random. image

You can respond whenever btw, you don’t need to respond immediately.

This is going to be the last time I respond for tonight but that’s not what I mean, what I mean is how do I get those position cords into the script, it is so confusing without any explanation

local spawns = {workspace.Spawn1, workspace.Spawn2, workspace.Spawn3}
local rand = math.random(1, #spawns)
local coin = game.ServerStorage:WaitForChild("Coin")

while task.wait(10) do
	local coinclone = coin:Clone()
	coinclone.CFrame = spawns[rand].CFrame
	coinclone.Parent = workspace
end

Some simple logic to achieve this, you’ll need to change it to fit your desires.

Its you again :eyes:

Anyway, is that a script for spawning coins randomly around the map?