#More than max coins are spawning

1 messages · Page 1 of 1 (latest)

pastel agate
#

So its supposed to stop spawning coins when MaxCoins is reached but it doesnt. (I think 2 or 3 coins will spawn which is more than MaxCoins)

#
function CoinSpawner:Spawn()
    task.spawn(function()
        while true do
            local character = self.player.Character or self.player.CharacterAdded:Wait()
            local root = character:WaitForChild("HumanoidRootPart")
            
            if CurrentCoins > MaxCoins then return end
            if CurrentCoins <= MaxCoins then
                CurrentCoins += 1
                
                local coin = Instance.new("Part")
                coin.Size = Vector3.new(1, 1, 1)
                coin.Shape = Enum.PartType.Ball
                coin.Anchored = false
                coin.CanCollide = false
                coin.Name = "Coin"
                coin.BrickColor = BrickColor.Random()
                coin.Parent = workspace:WaitForChild("Coins")
                
                local offset = Vector3.new(math.random(-50, 50), 2, math.random(-50, 50))
                coin.Position = root.Position + offset
                
                FollowPlayer(coin, root)
                
                coin.Touched:Connect(function(hit)
                    if hit.Parent:FindFirstChild("Humanoid") then
                        CurrentCoins -= 1
                        coin.CanTouch = false
                        coin:Destroy()
                        GiveCoinEvent:FireServer()
                    end
                end)
            end
            
            task.wait(1)
        end
    end)
end
lost dirge
#

that would stop the while true do

maiden harbor