Random Spawn Is Broken

Hi, I have a random coin spawn script, but no coins are spawning, and no errors are coming out in the output.

ServerScriptService

local coin = game.ReplicatedStorage.Coin -- Replace this with your coin model location
local coin_spawn_rate = 30 -- Coins spawn every 30 seconds
local min_coins = 10 -- How many coins will spawn at the minimum
local max_coins = 40 -- How many coins will spawn at the maximum

while wait(coin_spawn_rate) do
  for i = 1, math.random(min_coins, max_coins) do
      wait(.1)
      local coinClone = coin:Clone()
      
      coinClone.Position = Vector3.new(math.random(-250, 250), 100, math.random(-250, 250)) -- Makes coin spawn location random
      coin.Anchored = false
      coin.CanCollide = true
      coin.Parent = workspace
   end
end

ReplicatedStorage in a coin model

local coin = script.Parent
local currency = "Coins" -- rename to the currency that you want to use
local amt = 5 -- How many coins the player gets

local d = true

coin.Touched:Connect(function(p)
  if p.Parent:FindFirstChild("Humanoid") and d then
     d = false

    local plr = game.Players:FindPlayerFromCharacter(p.Parent)

    plr:FindFirstChild("leaderstats"):FindFirstChild(currency).Value =  plr:FindFirstChild("leaderstats"):FindFirstChild(currency).Value + amt

    coin:Destroy()
  end
end)

Thanks for reading.

Youre setting the original coins property instead of the cloned one. A fix for this would be

coinClone.Position = Vector3.new(math.random(-250, 250), 100, math.random(-250, 250)) -- Makes coin spawn location random
coinClone.Anchored = false
coinClone.CanCollide = true
coinClone.Parent = workspace
1 Like
      coin.Anchored = false
      coin.CanCollide = true
      coin.Parent = workspace

You are doing coinClone at the start, but then you’re unanchoring and parenting the original coin to Workspace.