I’m new at scripting and I’m trying to write this code as simply as possible to understand the process and I’m not particularly worried about the efficiency of the script. I’ve been working all day trying to create and then maintain 50 randomly located coins on a baseplate.
I’ve tried multiple loop methods: while true do, repeat until etc. The farthest I made it was actually creating 50 coins but not being able to instance more as the coins were picked up. I think that was with a specific while do true loop but I’ve tried so many variants in code I don’t even remember what the closest successful script was. I even tried using global variables to modify my coin count on touch from the part’s getCoin script when my count was stopping at 50 but it didn’t work.
Sorry this is super noob, after copying and learning aspects of coding for a few weeks this is my first attempt to actually code from nothing, I’ve been searching for spawning fixed numbers of instances but nothing is coming in as simple of terms that I’m looking for. Thanks for any information!
This current code is actually only spawning 10 coins with randomly increasing “Print Count” and it thinks 10 coins is 50. If I remove the getCount () from the loop it continues producing coins forever which is the problem in most of the code sequences I’ve tried.
local players = game:GetService("Players")
function playerSpawned(player)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local gold = Instance.new("IntValue", leaderstats)
gold.Name = "Gold"
gold.Value = 0
end
players.PlayerAdded:Connect(playerSpawned)
local coin = game.ServerStorage.Coin
local count = 0
local function getCount()
for _, v in pairs(workspace.Coins:GetChildren()) do
count=count+1
end
end
local function makeCoin()
local clone = coin:Clone()
clone.Parent = game.Workspace.Coins
clone.Position = Vector3.new(math.random(-226, 234), 2.834, math.random(-230, 231))
print(count)
end
if count<50 then repeat
makeCoin()
getCount()
wait()
until count >=50
end