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 4 years ago

Hi

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

for i, coin in pairs(AvailableCoinSpawnPoints) do
    if coin then
        currentRandomCoinNum = math.random(1, #AvailableCoinSpawnPoints)
        positionCoin = AvailableCoinSpawnPoints[currentRandomCoinNum]
        MyCoinTmp = ServerStorage.Coin
        MyCoin = MyCoinTmp:Clone()
        MyCoin.Parent = workspace
        MyCoin.CFrame = positionCoin.CFrame
        table.remove(AvailableCoinSpawnPoints,currentRandomCoinNum)
    end
end

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 4 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 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 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:

local CoinsFolder = ServerStorage:WaitForChild("Coins")

The parent for filling up the folder is like this:

MyCoin.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:

for i,v in pairs(CoinsFolder:GetChildren()) do
    v:Destroy()
   print("Coin DESTROYED: "..i)
end

ClonedMap: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