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?
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
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
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.
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.
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)
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
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.
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
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.
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.