Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I not spawn a coin on top of the already existing coins?

Asked by 5 years ago

Hi

In my game loop I spawn coins to points added to my map.

01for i, coin in pairs(AvailableCoinSpawnPoints) do
02    if coin then
03        currentRandomCoinNum = math.random(1, #AvailableCoinSpawnPoints)
04        positionCoin = AvailableCoinSpawnPoints[currentRandomCoinNum]
05        MyCoinTmp = ServerStorage.Coin
06        MyCoin = MyCoinTmp:Clone()
07        MyCoin.Parent = workspace
08        MyCoin.CFrame = positionCoin.CFrame
09        table.remove(AvailableCoinSpawnPoints,currentRandomCoinNum)
10    end
11end

This works fine the first time my game loops. When I have a winner the game quits and enters the lobby. When the game start over again the coins are spawn inside the already existing coins. And after a few rounds there are multiple coins spinning inside each other on the spawn point. I am thinking that AvailableCoinSpawnPoints would be refreshed when the map is destroyed (before the last player is sent to the lobby)?

Anyone know how I can spawn a fresh set of coins everytime a game starts?

2 answers

Log in to vote
1
Answered by 5 years ago

You would need to destroy all existing coins at the end of a round as well as the map. If you parent them to a Coins folder instead of workspace, then at the end of a round you can just iterate over the children of that folder to remove them all.

Also, in your script you are placing a coin for every available position, so using math.random to change the order that they are placed seems redundant. Just place them in order.

0
Thanks. I answered below. Tommmmmey 13 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Thanks for your answer!

Now I have created a folder in my ServerStorage named Coins. When I start up the test server I can see the folder have been filled up with my coins.

I get the folder like this:

1local CoinsFolder = ServerStorage:WaitForChild("Coins")

The parent for filling up the folder is like this:

1MyCoin.Parent = CoinsFolder

Since I now switched over from parent workspace to folder; the coins are not showing when I enter the map in test server. You know why this is?

Let's say that the coins are showing I thought I could add something like this at the end of the game:

1for i,v in pairs(CoinsFolder:GetChildren()) do
2    v:Destroy()
3   print("Coin DESTROYED: "..i)
4end
5 
6ClonedMap:Destroy()

(but this does not go into effect since no coin is showing on the map at the moment)

EDIT: I moved the Coins folder to workspace and now they appear in the map again and all the coins spawn just fine with no duplications.

Answer this question